0

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 (var MyURL) after click on link?

3 Answers 3

2

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"...

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.