Web Analytics Made Easy -
StatCounter JS for links - CodingForum

Announcement

Collapse
No announcement yet.

JS for links

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        I should add that the $domains should be a variable.

        Comment


        • #5
          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; Aug 23, 2011, 02: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.

          Comment


          • #6
            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.

            Comment


            • #7
              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.

              Comment


              • #8
                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?
                Be yourself. No one else is as qualified.

                Comment


                • #9
                  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?
                  Be yourself. No one else is as qualified.

                  Comment


                  • #10
                    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.

                    Comment


                    • #11
                      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>

                      Comment


                      • #12
                        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...
                        Be yourself. No one else is as qualified.

                        Comment


                        • #13
                          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.

                          Comment


                          • #14
                            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.
                            Be yourself. No one else is as qualified.

                            Comment


                            • #15
                              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.

                              Comment

                              Working...
                              X
                              😀
                              🥰
                              🤢
                              😎
                              😡
                              👍
                              👎