Solved

Use javascript to get the href value of a clicked link

Posted on 2010-08-19
2
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unexpected End of File 11 60
Validating Date 4 30
Jquery 7 34
Compute age Html 2 27
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

696 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