开发者

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();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜