Is this possible with Javascript?

Discussion in 'JavaScript' started by idotcom, Jun 17, 2008.

  1. #1
    Hi,

    I would like to add an exit popup window to several pages of a dynamic website. It has a lot of links that point to content in the site. I know that it's possible if you set like onclick='dopopup(), false' on the links that point within the site, so that if the browser is going to any other url the popup comes up.

    Is it possible to have javascript "listen" or check the url of a clicked link to see if it is pointing outside of the site? Like looking to see if href url is same as site??

    I'm trying to avoid having to change all of the href tags on the site as that would be a pain.

    Any help would be greatly appreciated. If you can provide a script solution I would be willing to pay for it.

    Thank you.
     
    idotcom, Jun 17, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    onload = function(){
    		var i = 0, aEls = document.getElementsByTagName('a'), l = aEls.length, click = function(){
    			alert('You are now leaving ' + location.host);
    		};
    		for ( ; i < l; i++){
    			if (!aEls[i].href.match(location.host))
    				aEls[i].addEventListener ? aEls[i].addEventListener("click", click, false) : aEls[i].attachEvent("onclick", click);
    		}
    };
    PHP:
    Just change the click function to do whatever you want.
     
    MMJ, Jun 18, 2008 IP
    idotcom likes this.
  3. gorantla

    gorantla Peon

    Messages:
    115
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i wil solve this 4 u
    abhi
     
    gorantla, Jun 18, 2008 IP
  4. idotcom

    idotcom Well-Known Member

    Messages:
    522
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Solved!

    Thanks MMJ
     
    idotcom, Jun 19, 2008 IP
  5. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    no problem. :)
     
    MMJ, Jun 19, 2008 IP