如何从 javascript 中转到新页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6321000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:17:05  来源:igfitidea点击:

How can I go to a new page from within javascript?

javascript

提问by Alexandre

I have a link like this:

我有一个这样的链接:

<a href="www.example.com">

I would like to simulate the clicking of this link within javascript with a function that is called with the left arrow but I am not sure how to do it.

我想用一个用左箭头调用的函数来模拟在 javascript 中点击这个链接,但我不知道该怎么做。

Now I have this:

现在我有这个:

$(window).keypress(function (e)
{
   if (e.keyCode == 37)
   {
    //  position 1
   }

});

But I am not sure how to change the page to the link "www.example.com" from within javascript at position 1. Can someone help me with this.

但我不确定如何将页面从 javascript 中的位置 1 更改为链接“www.example.com”。有人可以帮我解决这个问题吗?

回答by Darin Dimitrov

You could use the following to perform a redirect:

您可以使用以下内容来执行重定向:

window.location.href = 'www.example.com';

回答by Michael Robinson

You could set window.locationto the url you want the page to load, like:

您可以设置window.location为您希望页面加载的网址,例如:

window.location = "www.example.com"