How to store a url in variable after onclick event?
For example, in this piece of code:
<a href="www.some.html" onclick="...">www.some.html</a>
What I have write instead of three dots if I want to store the url (www.some.html) in variable (开发者_StackOverflow中文版var MyURL) after click on link?
When you click that link (and the onclick won't stop the default action) you will go to the link in href.
So to just store the link write this:
<a href="http://www.some.html" onclick="var a=this.href;return false">www.some.html</a>
the "return false" piece will stop the default action and prevent the browser from going to the url and you have a variable "a" with the value = "www.some.html"...
the variable you want is just this.href
- you can test it by setting your onclick to alert(this.href)
. Note that the anchor will work better with a full URL, ie http://www.some.html
The best way would be to use jquery
change link to...
<a href="www.some.html" id="myurl">www.some.html</a>
Then in jquery
$("#myurl").click(function(){
//set var
var MyURL = $(this).text();
});
精彩评论