Solved

get value of link using javascript

Posted on 2010-08-19
5
364 Views
Last Modified: 2013-11-19
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
Comment
Question by:elliottbenzle
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
ploftin earned 500 total points
ID: 33476522
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
 
LVL 2

Expert Comment

by:Drakecoldwinter
ID: 33476592
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
 
LVL 4

Author Comment

by:elliottbenzle
ID: 33476596
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
 
LVL 2

Expert Comment

by:Drakecoldwinter
ID: 33476616
Holy cow, this is the third time today someone post the solution just before me :(
0
 
LVL 2

Expert Comment

by:Drakecoldwinter
ID: 33476637
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now