使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
您需要在代码中输入这条线 :
$(location).attr("href","http://stackoverflow.com");
如果你没有jQuery, 和JavaScript一起去:
window.location.replace("http://stackoverflow.com");
window.location.href("http://stackoverflow.com");
其他回答
这是重定向到其他页面的代码 超过10秒的超时
<script>
function Redirect()
{
window.location="http://www.adarshkr.com";
}
document.write("You will be redirected to a new page in 10 seconds.");
setTimeout('Redirect()', 10000);
</script>
您也可以这样操作, 点击按钮时使用位置. 指派 :
<input type="button" value="Load new document" onclick="newPage()">
<script>
function newPage() {
window.location.assign("http://www.adarshkr.com")
}
</script>
但如果有人想重回主页 他可能会使用下面的片段
window.location = window.location.host
如果你有三种不同的环境,如发展、中转和生产,将会很有帮助。
您可以将这些单词放入铬控制台, 来探索此窗口或窗口。 位置对象 。 @ info: whatsthis火虫控制台
每个浏览器都使用此功能 :
window.location.href = 'your_url';
您可以使用以下方法重定向页面:
通过在头部使用元标记 -
<meta http-equiv="refresh" content="0;url=http://your-page-url.com" />
注意:content="0;
... 用于在您需要多少秒后重定向页面使用 JavaScript :
window.location.href = "http://your-page-url.com";
使用 jQuery 查询 :
$(location).attr('href', 'http://yourPage.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.
推荐文章
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?
- 我们可以在另一个JS文件中调用用一个JavaScript编写的函数吗?
- 如何使用JavaScript重新加载ReCaptcha ?
- jQuery。由于转义了JSON中的单引号,parseJSON抛出“无效JSON”错误
- 在JavaScript关联数组中动态创建键
- ReactJS和公共文件夹中的图像
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?