我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
ECMAScript 6 + jQuery,85位元
$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")
请不要杀了我,这是一个笑话,这是一个笑话,这是一个笑话。
这“提供了一个答案”,在这个意义上,它要求一个解决方案“使用jQuery”,这在这种情况下意味着以某种方式强迫它进入方程式。
Ferrybig 显然需要解释的笑话(仍然开玩笑,我确信在评论表格上有有限的选项),所以没有进一步的 ado:
其他答案是使用 jQuery 的 attr() 在位置或窗口对象不必要。
这个答案也滥用它,但更荒谬的方式. 而不是使用它来设置位置,它使用 attr() 恢复一个函数,设置位置。
“85比特”是指代码高尔夫,高尔夫显然不是你应该在代码高尔夫以外做的事情,而且这个答案显然不是真正的高尔夫。
基本上是克里斯蒂安。
其他回答
使用:
function redirect(a) {
location = a
}
求主引导他,求主引导他。
没有必要在位置后进行Href,因为它是有意义的。
我已经使用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>
如果您更喜欢使用纯粹的JavaScript,我意识到使用document.location.href = “https://example.com” 或 window.location.href = “https://example.com” 会导致 Firefox 中的兼容性问题。
location.href = "https://example.com";
location.replace("http://example.com");
在我看来,这个问题已经解决了,祝你好运!
您可以使用下面的方法重新引导页面:
使用头部中的 meta 标签 - <meta http-equiv="refresh" content="0;url=http://your-page-url.com" />. 请注意,内容="0;... 使用了几秒钟后你需要重新引导页面 使用 JavaScript: window.location.href = "http://your-page-url.com"; 使用 jQuery: $(location).attr('href', 'http://yourPage.com/');
此分類上一篇: https://jquery.com/browser-support/
<!DOCTYPE html>
<html>
<head>
<title>example</title>
</head>
<body>
<p>You will be redirected to google shortly.</p>
<script>
setTimeout(function(){
window.location.href="http://www.google.com"; // The URL that will be redirected too.
}, 3000); // The bigger the number the longer the delay.
</script>
</body>
</html>
不同选项如下:
window.location.href="url"; // Simulates normal navigation to a new page
window.location.replace("url"); // Removes current URL from history and replaces it with a new URL
window.location.assign("url"); // Adds new URL to the history stack and redirects to the new URL
window.history.back(); // Simulates a back button click
window.history.go(-1); // Simulates a back button click
window.history.back(-1); // Simulates a back button click
window.navigate("page.html"); // Same as window.location="url"
您也可以使用 meta 数据进行页面重定向如下。
梅塔新鲜
<meta http-equiv="refresh" content="0;url=http://evil.example/" />
地图位置
<meta http-equiv="location" content="URL=http://evil.example" />
<base href="http://evil.example/" />
在此页面上可以找到许多其他方法将您的无疑客户端重新引导到他们可能不想去的页面(其中没有一个依赖于jQuery):
https://code.google.com/p/html5security/wiki/Redirection方法
您也可能被报告为恶意网站. 如果发生的话,当人们点击到您的网站的链接时,用户浏览器可能会警告他们您的网站是恶意的。
<!DOCTYPE html>
<html>
<head>
<title>Go Away</title>
</head>
<body>
<h1>Go Away</h1>
<script>
setTimeout(function(){
window.history.back();
}, 3000);
</script>
</body>
</html>
如果你将两页的例子结合在一起,你会有一个婴儿路由,这将保证你的用户永远不会想再使用你的网站。