Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: JS for links

  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS for links

    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:

    Code:
    $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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Is this what you mean?

    Code:
    <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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I should add that the $domains should be a variable.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Quote Originally Posted by nugg79 View Post
    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.
    Last edited by Philip M; 08-23-2011 at 08:18 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    I do not see what is wrong with a simple alert raised by onclick in the appropriate links:-

    Code:
    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.");
    }

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,677
    Thanks
    80
    Thanked 4,644 Times in 4,606 Posts
    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
    Code:
         <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?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,677
    Thanks
    80
    Thanked 4,644 Times in 4,606 Posts
    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?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by nugg79 View Post
    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.

  • #11
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by nugg79 View Post
    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:
    Code:
    <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>

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,677
    Thanks
    80
    Thanked 4,644 Times in 4,606 Posts
    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...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Old Pedant View Post
    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.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,677
    Thanks
    80
    Thanked 4,644 Times in 4,606 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Old Pedant View Post
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •