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.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    javascript external link

    Hi, is it possible to make a external javascript link by giving all the external
    links a class name then use getelementbytagname in the js file with
    a function to alert the hes leaving the website and let him choose if
    he wants to stay or leave.
    well thats what im trying to do but i dont know how to write the
    function for it maybe i could get a suggestion or a link to agood tutorial.

    / thanks lisa

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    You may set a class attribute on those anchors and use getElementsByClassName() method to fetch all such links. Then you may add an event-listener for them for the click event, see http://www.quirksmode.org/js/events_advanced.html
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Ok thanks

    Ok thanks but i really got to see a example how its done im to much of a beginner to write the js function myself.
    check your mail soon by the way i sent you a mail.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <a href="http://www.google.com" class="external_link">Google</a>
    <a href="http://www.yahoo.com" class="external_link">Yahoo</a>
    ...and so on...
    <script type="text/javascript">
    function setPromptOnExternalLinks(){
      var links = document.links;
      for (var i=0; i<links.length;i++){
         if (links[i].className.indexOf("external_link") != -1){
            links[i].onclick = function(){
               return prompt("You are about to go to an external website, do you want to continue?");
            }
         }
      }
    }
    
    setPromptOnExternalLinks(); //you may call this function onload if you want
    </script>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Quote Originally Posted by glenngv View Post
    [CODE return prompt("You are about to go to an external website, do you want to continue?");
    [/CODE]
    Surely should be:-

    return confirm("You are about to go to an external website, do you want to continue?");

    I am tempted to respond to the prompt box by typing in something like
    "Yes, of course I want to continue, you nerd. Otherwise I would not have clicked on the external link."
    Last edited by Philip M; 12-04-2009 at 09:23 AM.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Philip M View Post
    Surely should be:-

    return confirm("You are about to go to an external website, do you want to continue?");

    I am tempted to respond to the prompt box by typing in something like
    "Yes, of course I want to continue, you nerd. Otherwise I would not have clicked on the external link."
    confirm is the right one. I didn't know why I used prompt
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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