如何使用 JavaScript 重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4744751/
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-08-23 13:52:41  来源:igfitidea点击:

How do I redirect with JavaScript?

javascriptredirect

提问by Lucky13

How do you redirect to a page from another page with JavaScript?

如何使用 JavaScript 从另一个页面重定向到一个页面?

回答by seedg

To redirect to another page, you can use:

要重定向到另一个页面,您可以使用:

window.location = "http://www.yoururl.com";

回答by sidanmor

window.location.replace('http://sidanmor.com');

It's betterthan using window.location.href = 'http://sidanmor.com';

这比使用更好window.location.href = 'http://sidanmor.com';

Using replace()is better because it does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.

使用replace()更好,因为它不会在会话历史记录中保留原始页面,这意味着用户不会陷入永无止境的后退按钮惨败中。

If you want to simulate someone clicking on a link, use window.location.href

If you want to simulate an HTTP redirect, use window.location.replace

如果要模拟某人单击链接,请使用 window.location.href

如果要模拟 HTTP 重定向,请使用 window.location.replace

For example:

例如:

// similar behavior as an HTTP redirect
window.location.replace("http://sidanmor.com");

// similar behavior as clicking on a link
window.location.href = "http://sidanmor.com";

Taken from here:How to redirect to another page in jQuery?

取自此处:如何在 jQuery 中重定向到另一个页面?

回答by Shadow Wizard is Ear For You

You can't redirect to a function. What you can do is pass some flag on the URL when redirecting, then check that flag in the server side code and if raised, execute the function.

你不能重定向到一个函数。您可以做的是在重定向时在 URL 上传递一些标志,然后在服务器端代码中检查该标志,如果引发,则执行该函数。

For example:

例如:

document.location = "MyPage.php?action=DoThis";

Then in your PHP code check for "action" in the query string and if equal to "DoThis" execute whatever function you need.

然后在您的 PHP 代码中检查查询字符串中的“action”,如果等于“DoThis”,则执行您需要的任何函数。

回答by Kip

  • If you want to simulate someone clicking on a link, use location.href.
  • If you want to simulate an HTTP redirect, use location.replace.
  • 如果要模拟某人单击链接,请使用location.href.
  • 如果要模拟 HTTP 重定向,请使用location.replace.

For example:

例如:

// Similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// Similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

Information copied from this answer to a duplicate question

从此答案复制到重复问题的信息

回答by Fenton

You may need to explain your question a little more.

您可能需要多解释一下您的问题。

When you say "redirect", to most people that suggest changing the location of the HTML page:

当您说“重定向”时,对于大多数建议更改 HTML 页面位置的人:

window.location = url;

When you say "redirect to function" - it doesn't really make sense. You can call a function or you can redirect to another page.
You can even redirect and have a function called when the new page loads.

当你说“重定向到功能”时 - 它真的没有意义。您可以调用函数,也可以重定向到另一个页面。
您甚至可以重定向并在新页面加载时调用一个函数。

回答by Zeus

Compared to window.location="url";it is much easyer to do just location="url";I always use that

相比之下,window.location="url";它更容易做到,只是location="url";我总是使用它