get value of link using javascript

I have these two links:

<div class="leave"><a href="">Leave to Google</a></div>
<div class="leave"><a href="">Leave to Yahoo</a></div>

When the user leaves the page this function fires:

function Redirect() {
var where="colorbox_autofire.html";

through the body onUnLoad:
<body onUnLoad="Redirect()">
Before the user is redirected I want to get the href value of the link they clicked and store it in a variable. How would I do this?

Who is Participating?
ploftinConnect With a Mentor Commented:
I'm not sure what the redirection to colorbox_autofire.html is supposed to do, but if I ignore that piece for now, how about something like this:
<div class="leave"><a href="Javascript:;" onClick="Redirect('')">Leave to Google</a></div>
<div class="leave"><a href="Javascript:;" onClick="Redirect('')">Leave to Yahoo</a></div>

<script language="JavaScript">
function Redirect(myLocation) {
//   var where = "colorbox_autofire.html";
//   window.location=where;

// myLocation is a variable that now has the link that was clicked.
   window.location = myLocation;

Open in new window

You should do a complete work arround to get to your solution. Try this instead:

function redirect(clickedlink)
// currently you have the clicked link in a var named clickedlink
var where="colorbox_autofire.html";

Then you do your links like this:

<div class="leave"><a onclick="javascript:redirect('')" style="cursor:hand">Leave to Google</a></div>
<div class="leave"><a onclick="javascript:redirect('')" style="cursor:hand">Leave to Yahoo</a></div>

I even added some CSS so that the cursor reacts well to the link
elliottbenzleAuthor Commented:
This was what I thought of too. But I'm not in control of the links. I need to be able to get that value ( without applying anything to the link itself. I was thinking of something like:


does this make any sense?
Holy cow, this is the third time today someone post the solution just before me :(
Maybe better to check the History object in Javascript ?

check the previos attribute ;)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.