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


当前回答

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

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

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

其他回答

有很多方法可以做到这一点。

// window.location
window.location.replace('http://www.example.com')
window.location.assign('http://www.example.com')
window.location.href = 'http://www.example.com'
document.location.href = '/path'

// window.history
window.history.back()
window.history.go(-1)

// window.navigate; ONLY for old versions of Internet Explorer
window.navigate('top.jsp')


// Probably no bueno
self.location = 'http://www.example.com';
top.location = 'http://www.example.com';

// jQuery
$(location).attr('href','http://www.example.com')
$(window).attr('location','http://www.example.com')
$(location).prop('href', 'http://www.example.com')

您可以使用 jQuery 如下:

window.location = "http://yourdomain.com";

如果你只想要jQuery,那么你可以这样做:

$jq(window).attr("location","http://yourdomain.com");

这是很容易实现的,你可以使用:

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

这将记住前一页的历史,所以可以通过点击浏览器的背面按钮返回。

或:

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

此方法不记得前一页的历史,此情况下后按钮将被禁用。

警告:这个答案只是作为一个可能的解决方案提供;它显然不是最好的解决方案,因为它需要jQuery。

$(location).prop('href', 'http://stackoverflow.com')

如果您想在同一应用程序中重新引导到路线

window.location.pathname = '/examplepath'

这将是走路的方式。