Solved

Use javascript to get the href value of a clicked link

Posted on 2010-08-19
2
490 Views
Last Modified: 2013-11-19
Is there a way to use javascript to get the href value of a link that was clicked when onUnLoad is fired? I want to create a function that runs when a page unloads and somehow get the href value of the link that user clicked to leave the page.

So like this:
<script>
function Redirect() {
var linkclicked="this will be the value of whatever link was clicked";
}
</script>

<body onUnLoad="Redirect()">

<div class="leave"><a href="http://www.google.com">Leave to Google</a></div>

and when the user clicks "Leave to Google" the value "http://www.google.com" will be put into the variable "linkclicked"  
but the tricky part is that I can't add anything to the link itself. The solution must be contained within the javascript function.

Thanks.
0
Comment
Question by:elliottbenzle
2 Comments
 
LVL 15

Accepted Solution

by:
SRigney earned 250 total points
ID: 33477180
with jQuery I would do this.

$("a").click(function() {
   // do what you want with the value.
  $(this).attr("href");
});


The same thing can be done with straight javascript, but I'm more of a jQuery expert.  The click event should fire before the page unloads.
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 250 total points
ID: 33477548
This is a very basic example what can be done:


<script>
var linkclicked
function Redirect() {
  alert("linkclicked: "+linkclicked);
}

function addClicks(){
  var lnk = document.links;
  for(var i=0;i<lnk.length;i++){
    lnk[i].onclick=function(){linkclicked=this.href;}
  }
}
</script>

<body onLoad="addClicks()" onUnLoad="Redirect()">

<div class="leave"><a href="http://www.google.com">Leave to Google</a></div>


0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to dynamically set the form action using jQuery.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question