• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

get value of link using javascript

I have these two links:

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

When the user leaves the page this function fires:

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

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?

Thanks
0
elliottbenzle
Asked:
elliottbenzle
  • 3
1 Solution
 
ploftinCommented:
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('http://www.google.com')">Leave to Google</a></div>
<div class="leave"><a href="Javascript:;" onClick="Redirect('http://www.yahoo.com')">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.
   alert(myLocation);
   window.location = myLocation;
}
//-->
</script>

Open in new window

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

<script>
function redirect(clickedlink)
{
// currently you have the clicked link in a var named clickedlink
var where="colorbox_autofire.html";
window.location=where;
}
</script>

Then you do your links like this:

<div class="leave"><a onclick="javascript:redirect('http://www.google.com')" style="cursor:hand">Leave to Google</a></div>
<div class="leave"><a onclick="javascript:redirect('http://www.yahoo.com')" style="cursor:hand">Leave to Yahoo</a></div>

I even added some CSS so that the cursor reacts well to the link
0
 
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 (http://www.google.com) without applying anything to the link itself. I was thinking of something like:

document.a.href.clickedvalue

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

http://www.comptechdoc.org/independent/web/cgi/javamanual/javahistory.html

check the previos attribute ;)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now