Grab the URL of the clicked link on page unload

Discussion in 'JavaScript' started by tflynn25, Oct 22, 2008.

  1. #1
    Once a link is clicked, how can I grab the href attribute of it?

    For my specific project, it must be done using the 'unload' event in jQuery.

    <script type="text/javascript">
    $(window).unload( function () {
    	alert( window.document.URL );
    } );
    </script>
    Code (markup):
    window.document.URL and window.location.href both return the current domain. I want it to show the domain that is about to load.
     
    tflynn25, Oct 22, 2008 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi, I don't know exactly how with jQuery, but without, you probably need to attach event onclick to all links that can take you from the page. Something like
    var a = document.getElementsByTagName("a");
    var l = a.length;
    for(var i=0; i<l; i++) {
    a.onclick = function() { alert(a.href) }
    }
    Of course you have to filter the links, so you don't attach this on every link, just those that are pointing outside your current domain(window.location.href)
     
    lp1051, Oct 23, 2008 IP
  3. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #3
    How about this:

    
    <html>
    <body>
    <SCRIPT>
    var lastLink = "closedOut";
    
    function setLastLink(theHref){
    	lastLink = theHref;
    }
    
    window.onbeforeunload = function(event){
    	if(lastLink != "closedOut"){
    		alert("Leaving to: " + lastLink + "! Bye!");
    	}
    }
    </SCRIPT>
    <A HREF="http://www.bubblecoder.com/" onClick=setLastLink(this.href);>BubbleCoder!!!!</A>
    </body>
    </html>
    
    Code (markup):
    Tested working in both FireFox v3.SomethingOrOther and what ever the latest crappy version of IE is. . .

    (And yea, it's not jQuery, but you can change it. . . Dunno why your using jQuery anyway >.>)
     
    ToddMicheau, Oct 23, 2008 IP
  4. salahsoftware

    salahsoftware Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The solution of Todd is working fine. I tested it in my system.
     
    salahsoftware, Oct 24, 2008 IP
  5. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Yeah, me too, even on older versions, great tip Todd. Where did you find this non-standard event?
     
    lp1051, Oct 25, 2008 IP
  6. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #6
    You mean window.onbeforeunload()? That's standard. The rest is a simple function that just sets a variable to the href property of the clicked link.

    I can also make you a JS function that will loop through every link on the page and set the onclick function- that way you don't have to do it manually. Just embed the JS.
     
    ToddMicheau, Oct 25, 2008 IP
  7. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #7
    Yes, I mean window.onbeforeunload(). I said non-standard, because I didn't find it in DOM 2 model or by W3c - JavaScript Event Reference. I know there's many events outside the scope of those documents, however I was just interested where did you come to this onbeforeunload.
     
    lp1051, Oct 26, 2008 IP