开发者

use variable with window.location.href

I was wondering how I would be able to execute a command such as this in javascript. I just want to store the url as a string in a variable and redirect them to it when the time comes -

var linkz = "http://www.google.com/";
window.location.href= linkz;

Why doesn't开发者_运维问答 this work?

Thanks,


If you're using links this way (as mentioned in a comment):

<a href="javascript:checkCk(google.com)">Google</a>

Then the problem is that you're not passing a string to your checkCk() function.

Try this:

<a href="javascript:checkCk('http://google.com')">Google</a>

The code you used for window.location.href should work.

At this point, I don't see why you'd use javascript if all you're doing is replacing the default behavior of the link.


I've just tried on my local machine, and it works:

<script>

window.onload = function(){
    var google = "http://google.com";
    window.location.href = google;
}
</script>
Redirecting to google...

Copy this to new file, call it for example redirect.html and open it in your browser.

Update:

<script>

var redirect = function(new_place) {
    window.location.href = new_place;
}

</script>
<a href='javascript:redirect("http://google.com")'>Go to google</a>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜