Solved

Use javascript to get the href value of a clicked link

Posted on 2010-08-19
2
487 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
2 Comments
 
LVL 15

Accepted Solution

by:
SRigney earned 250 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

18 Experts available now in Live!

Get 1:1 Help Now