Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Use javascript to get the href value of a clicked link

Posted on 2010-08-19
2
Medium Priority
?
506 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 1000 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 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

715 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