php 如何在 jQuery 中使用标题位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20919998/
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
How do I use a header location in jQuery?
提问by Deepu Sasidharan
Can I use a header location in jQuery for redirection or refresh?
我可以在 jQuery 中使用标题位置进行重定向或刷新吗?
Like in PHP:
就像在 PHP 中一样:
header('location:www.google.co.in');
header("Refresh:1,url=home.php");
If not, what is the alternative way?
如果没有,替代方法是什么?
回答by Martin
Headers are interpreted prior to the rendering of the document, and before jQuery is even loaded so these aren't an option. Instead, you can redirect the browser using document.location.
标题在呈现文档之前被解释,甚至在 jQuery 被加载之前被解释,所以这些不是一个选项。相反,您可以使用document.location.
document.location.href = 'www.google.co.in';
For a jQuery approach you can use
对于 jQuery 方法,您可以使用
$(location).attr('href', 'www.google.co.in');
however I would favor the plain javascript version.
但是我更喜欢纯javascript版本。
回答by Stefanos Vakirtzis
You can use:
您可以使用:
window.location.assign('www.google.co.in');
or
或者
window.location.href = 'www.google.co.in';
or
或者
window.location.replace('www.google.co.in');
The difference is that assign()will just cause a new document to load. While replace()will replace the current document and replace the current history with that URL making it so you can't go back to the previous document loaded.
不同之处在于,assign()这只会导致加载新文档。Whilereplace()将替换当前文档并用该 URL 替换当前历史记录,从而使您无法返回上一个加载的文档。
回答by Jenz
Use
用
window.location.href="www.google.co.in";
for redirection.
用于重定向。
and
和
window.location.reload(true);
to reload the page
重新加载页面

