...

View Full Version : JS for links



nugg79
08-23-2011, 05:56 PM
I am trying to build a function that when a link is checked, the js will check it for its top level domain and if it is other than a .gov, will produce an alert. What I have come up with is the following:


$domains = array(".aero",".biz",".cat",".com",".coop",".edu",".info",".int",".jobs",".mil",".mobi",".museum",
".name",".net",".org",".travel",".ac",".ad",".ae",".af",".ag",".ai",".al",".am",".an",".ao",".aq",".ar",".as",".at",".au",".aw",
".az",".ba",".bb",".bd",".be",".bf",".bg",".bh",".bi",".bj",".bm",".bn",".bo",".br",".bs",".bt",".bv",".bw",".by",".bz",".ca",
".cc",".cd",".cf",".cg",".ch",".ci",".ck",".cl",".cm",".cn",".co",".cr",".cs",".cu",".cv",".cx",".cy",".cz",".de",".dj",".dk",".dm",
".do",".dz",".ec",".ee",".eg",".eh",".er",".es",".et",".eu",".fi",".fj",".fk",".fm",".fo",".fr",".ga",".gb",".gd",".ge",".gf",".gg",".gh",
".gi",".gl",".gm",".gn",".gp",".gq",".gr",".gs",".gt",".gu",".gw",".gy",".hk",".hm",".hn",".hr",".ht",".hu",".id",".ie",".il",".im",
".in",".io",".iq",".ir",".is",".it",".je",".jm",".jo",".jp",".ke",".kg",".kh",".ki",".km",".kn",".kp",".kr",".kw",".ky",".kz",".la",".lb",
".lc",".li",".lk",".lr",".ls",".lt",".lu",".lv",".ly",".ma",".mc",".md",".mg",".mh",".mk",".ml",".mm",".mn",".mo",".mp",".mq",
".mr",".ms",".mt",".mu",".mv",".mw",".mx",".my",".mz",".na",".nc",".ne",".nf",".ng",".ni",".nl",".no",".np",".nr",".nu",
".nz",".om",".pa",".pe",".pf",".pg",".ph",".pk",".pl",".pm",".pn",".pr",".ps",".pt",".pw",".py",".qa",".re",".ro",".ru",".rw",
".sa",".sb",".sc",".sd",".se",".sg",".sh",".si",".sj",".sk",".sl",".sm",".sn",".so",".sr",".st",".su",".sv",".sy",".sz",".tc",".td",".tf",
".tg",".th",".tj",".tk",".tm",".tn",".to",".tp",".tr",".tt",".tv",".tw",".tz",".ua",".ug",".uk",".um",".us",".uy",".uz", ".va",".vc",
".ve",".vg",".vi",".vn",".vu",".wf",".ws",".ye",".yt",".yu",".za",".zm",".zr",".zw");
if ($domains == true)
{
alert();
}
else
{
//do nothing
}


I am not truly fluent with js and need help with the function. My idea is to get away from having to apply an onclick/onkeypress to each link and apply this code externally or in line to each page.

Any suggestions?

Thanks
Alan

Philip M
08-23-2011, 06:40 PM
Is this what you mean?


<script type = "text/javascript">

var lk = "http://www.google.co.uk"; // the link
if (!/(\.gov)/gi.test(lk)) {
alert ("The link is not a .gov domain");
}
else {
alert ("That is a .gov domain");
}

</script>

Quizmaster: The coastal towns of Whitby and Scarborough lie next to which body of water?
Contestant: The Atlantic

nugg79
08-23-2011, 06:49 PM
Without running the script it looks like it could work. This code would provide an alert only for sites that have a .com, .net, .org, etc but not for .gov correct. I am trying to avoid using the onclick for each link. I want to be able to add the function at the top of the page and the function acts on all links without having to be called on individual links.

nugg79
08-23-2011, 06:53 PM
I should add that the $domains should be a variable.

Philip M
08-23-2011, 07:15 PM
Without running the script it looks like it could work. This code would provide an alert only for sites that have a .com, .net, .org, etc but not for .gov correct. I am trying to avoid using the onclick for each link. I want to be able to add the function at the top of the page and the function acts on all links without having to be called on individual links.

I don't really understand this. The links pre-defined in your page are either.gov domains or they are not. Why is it necessary to check it? Or are we talking about some URL which is input by the user? And you must specify the action to be taken for each URL onclick.

nugg79
08-23-2011, 07:32 PM
My client is a government client and requires all links that are not government websites to have a disclaimer letting the user know that they are leaving a government site and therefore not responsible for the information on those websites that are not government.

Philip M
08-23-2011, 08:54 PM
I do not see what is wrong with a simple alert raised by onclick in the appropriate links:-


function alertUser() {
alert ("You are leaving this government site and you are reminded that the goverment is not responsible for the information contained in those websites that are not government.");
}

Old Pedant
08-23-2011, 11:52 PM
I think, Philip, he wants to have code that will add the "checkForNonGov()" call to all existing links in the page.

nugg79: That's easy enough *IF* links are only made via <a href="..."> and <form action="..."> tags. But realize that it's also possible to have code such as

<div onclick="someFunctionThatMayGoAnywhere()"> ...

And, worse, it possible to have JS code that will add such onclick (or onmouseover or or or) handlers dynamically, so you won't even see them in the code.

So... How extreme do you need/want to push this?

Old Pedant
08-23-2011, 11:54 PM
Oh...and let's say that your client is the USA government and a website is "www.SyrianNation.gov". Does that mean that it's okay to go to *that* ".gov" site?

webdev1958
08-23-2011, 11:57 PM
My client is a government client and requires all links that are not government websites to have a disclaimer letting the user know that they are leaving a government site and therefore not responsible for the information on those websites that are not government.

I would do something like assign a class govLnk to the government links in the html. That should be easy and straight forward. Then use getElementsByClassName() to collect those government links automatically when the page loads and assign an onclick event handler function to display a stylised popup container to display uour message.

With this, you can add or remove links in the html without having to modify any javascript code.

btw, whether you use an alert box or a stylised container is a personal choice. But for me, alert boxes look amateurish.

Logic Ali
08-24-2011, 02:06 AM
My idea is to get away from having to apply an onclick/onkeypress to each link and apply this code externally or in line to each page.
I advised you in another place that there is a server-based alternative that doesn't require script support, but here's a solution that only uses one event handler, can be installed anywhere and can't be overwritten:
<script type='text/javascript'>

(function()
{
var ef = document.attachEvent || document.addEventListener, ael = !!window.addEventListener;

ef( (ael ? 'click' : 'onclick'), function( e )
{
var evt = e || window.event,
elem = evt.srcElement || evt.target,
isLink = false,
go = true;

while( elem && elem.nodeName != 'A' )
elem = elem.parentNode;

if( elem && !/\:\/\/.+\.gov(\/|$)/.test( elem.href ) && !( go = confirm( "OK to navigate to an external website?" ) ) )
if ( evt.preventDefault )
evt.preventDefault();

evt.returnValue = go;

}, false );

})();

</script>

Old Pedant
08-24-2011, 02:15 AM
Ummm...yes, LogicAli, but what about <form> actions? And what about JS code that is invoked via onclick or onmouseover or maybe even just via a setTimeout? And more...

Logic Ali
08-24-2011, 02:59 AM
Ummm...yes, LogicAli, but what about <form> actions? And what about JS code that is invoked via onclick or onmouseover or maybe even just via a setTimeout? And more...Presumably this is a controlled environment that happens to contain some selected external links.
In the unlikely event of external navigation being invoked by other means, precautions can be handled by the developer responsible.

Old Pedant
08-24-2011, 03:22 AM
100% agreed. But then by that same token, if it's a controlled environment, why not change the HTML (or PHP or whatever) coding to insert the calls rather than doing it ex post facto?

Doing it as you show (and even extending that to look for <form action=>) just seems to me to be putting a band-aid on the problem. Not that it's not a working solution, just a hack that shouldn't need to be there.

Logic Ali
08-24-2011, 03:52 AM
100% agreed. But then by that same token, if it's a controlled environment, why not change the HTML (or PHP or whatever) coding to insert the calls rather than doing it ex post facto?

Doing it as you show (and even extending that to look for <form action=>) just seems to me to be putting a band-aid on the problem. Not that it's not a working solution, just a hack that shouldn't need to be there.

As I mentioned in my first post, in another reply to this crosspost I suggested pointing all the links at an advisory page, with the final URL appended as a parameter, so that 'Continue' and 'Return' links could be generated. If that can't be done then a single maintenance free routine has to be the next best thing.

Old Pedant
08-24-2011, 03:56 AM
I *like* that idea! Yes, send everything to the advisory page, which could be a server-side page that would then just pass on through to "in house" pages. Takes care of the "www.SyrianNation.gov" that I mentioned, too. Good one.

Philip M
08-24-2011, 07:31 AM
Perhaps I am a little thick today, but I do not see why a US Government website would include a link to "www.SyrianNation.gov" or similar. What about "www.al-quaeda.org"?

nugg79
08-24-2011, 03:31 PM
I think, Philip, he wants to have code that will add the "checkForNonGov()" call to all existing links in the page.

nugg79: That's easy enough *IF* links are only made via <a href="..."> and <form action="..."> tags. But realize that it's also possible to have code such as

<div onclick="someFunctionThatMayGoAnywhere()"> ...

And, worse, it possible to have JS code that will add such onclick (or onmouseover or or or) handlers dynamically, so you won't even see them in the code.

So... How extreme do you need/want to push this?


The site is a pure html site, no backend coding. Currently there is an alert that requires an onclick and onkeypress(Assessibility) on each link. The client will not allow an elaborate code to be added. I was trying to get a js code that will catch all none gov sites.

Logic Ali
08-24-2011, 04:03 PM
The site is a pure html site, no backend coding. Currently there is an alert that requires an onclick and onkeypress(Assessibility) on each link. The client will not allow an elaborate code to be added. I was trying to get a js code that will catch all none gov sites.
I gave you a single unobtrusve routine that will do that. Now it seems you have some other code already in place.
You need to show an example of this existing alert code.

nugg79
08-24-2011, 04:23 PM
I advised you in another place that there is a server-based alternative that doesn't require script support, but here's a solution that only uses one event handler, can be installed anywhere and can't be overwritten:
<script type='text/javascript'>

(function()
{
var ef = document.attachEvent || document.addEventListener, ael = !!window.addEventListener;

ef( (ael ? 'click' : 'onclick'), function( e )
{
var evt = e || window.event,
elem = evt.srcElement || evt.target,
isLink = false,
go = true;

while( elem && elem.nodeName != 'A' )
elem = elem.parentNode;

if( elem && !/\:\/\/.+\.gov(\/|$)/.test( elem.href ) && !( go = confirm( "OK to navigate to an external website?" ) ) )
if ( evt.preventDefault )
evt.preventDefault();

evt.returnValue = go;

}, false );

})();

</script>


I saw your post in the other forum. I was trying to get as much help on this as possible. The client changed their mind on how they want to do things now which renders my idea useless. This is what I was trying to do so that I could place in the file and it would be a catch all for existing code from past contractors. Thank you very much guys.

Old Pedant
08-24-2011, 09:40 PM
Perhaps I am a little thick today, but I do not see why a US Government website would include a link to "www.SyrianNation.gov" or similar. What about "www.al-quaeda.org"?

What, you've never heard of hackers getting into government web sites?

[My example was, shall we say, a bit extreme on purpose. I was really wondering if he needed to show alerts when moving from one governmental entity to another. But I like your example even better.]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum