0

I want to pass url parameter through html dom. This is the javascript i am using

<script>
function myFunction(url)
{
setTimeout(function(){window.location.assign(url)},4000);
}
</script>

HTML

<a href="#" onclick="myFunction(http://stackoverflow.com)"> Click here </a>

But its not working. How can i fix this situation ? Thanks for your answers.

3
  • You should try a little more before ask. That's a easy one and understanding it is part of learning how the language works. -1 Commented Jan 3, 2014 at 11:36
  • I am completely new to javascript thats why i asked Commented Jan 3, 2014 at 11:38
  • I realized that, but just asking won't sum to your learning than searching, reading the docs, learning how to understand errors and etc. See these topics(choose according to your fav browser). Commented Jan 3, 2014 at 11:41

3 Answers 3

1

Use combination of single and double quotes, to pass the url as string literal. If the url is not enclosed in quotes it is considered as some variable and javascript could nod find that.

<a href="#" onclick="myFunction('http://stackoverflow.com')"> Click here </a>
Sign up to request clarification or add additional context in comments.

Comments

0

As @Adil says, you need to pass the argument as string to the function. In the way you are trying, the script is trying to recover the value of the http://stackoverflow.com variable.

Comments

0

what @Adil has said is true:

<a href="#" onclick="myFunction('http://stackoverflow.com')"> Click here </a>

but if you want to pass the url as a parameter do not forget to change your function like this:

function myFunction(url)
{
    setTimeout(function(){window.location.assign(encodeURIComponent(url)},4000);
}

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.