我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
首先写得正确. 你想在一个应用程序内导航另一个链接从你的应用程序另一个链接. 这里是代码:
window.location.href = "http://www.google.com";
如果你想在你的应用程序内浏览页面,那么我也有代码,如果你愿意。
其他回答
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.
但是,如果有人想重新引导到主页,那么他可以使用下面的剪辑。
window.location = window.location.host
如果你有三种不同的环境,如发展、排行和生产,这将是有用的。
您可以通过在 Chrome 控制台或 Firebug 的控制台中放置这些单词来探索此窗口或 window.location 对象。
在JavaScript中,您可以通过使用以下方式重新引导到一个特定的页面:
window.location.replace("http://www.test.com");
或
location.replace("http://www.test.com");
或
window.location.href = "http://www.test.com";
使用 jQuery:
$(window).attr("location","http://www.test.com");
您需要在代码中插入此行:
$(location).attr("href","http://stackoverflow.com");
如果您没有 jQuery,请使用 JavaScript:
window.location.replace("http://stackoverflow.com");
window.location.href("http://stackoverflow.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')