我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?


当前回答

有三种主要方法可以做到这一点,

window.location.href='blaah.com';
window.location.assign('blaah.com');

和...

window.location.replace('blaah.com');

最后一个是最好的,对于一个传统的重定向,因为它不会保存你去的页面,然后在你的搜索历史中重定向。

EDIT:窗口预定是可选的。

其他回答

你可以使用:

window.location.replace("http://www.example.com/");

替代() 方法不会在会议历史中保存起源页面,因此用户无法使用后按钮返回并重新转向。

但是,如果你想要一个与点击链接相同的效果,你应该去:

window.location.href = "http://www.example.com/";

在此情况下,浏览器返回按钮将是活跃的。

如果您更喜欢使用纯粹的JavaScript,我意识到使用document.location.href = “https://example.com” 或 window.location.href = “https://example.com” 会导致 Firefox 中的兼容性问题。

location.href = "https://example.com";
location.replace("http://example.com");

在我看来,这个问题已经解决了,祝你好运!

var url = 'asdf.html';
window.location.href = url;

JavaScript 是非常广泛的. 如果你想跳到另一个页面,你有三个选项。

 window.location.href='otherpage.com';
 window.location.assign('otherpage.com');
 //and...

 window.location.replace('otherpage.com');

当你想移动到另一个页面,你可以使用其中一个,如果这是你的要求. 但是,所有三个选项都限于不同的情况。

如果你有兴趣了解更多关于这个概念,你可以通过更多。

window.location.href; // Returns the href (URL) of the current page
window.location.hostname; // Returns the domain name of the web host
window.location.pathname; // Returns the path and filename of the current page
window.location.protocol; // Returns the web protocol used (http: or https:)
window.location.assign; // Loads a new document
window.location.replace; // RReplace the current location with new one.

我已经使用JavaScript的重定向功能(),它工作了。

<script type="text/javascript">
    $(function () {
        //It's similar to HTTP redirect
        window.location.replace("http://www.Technomark.in");

        //It's similar to clicking on a link
        window.location.href = "Http://www.Technomark.in";
    })
</script>