使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
应该能够设定使用window.location
.
示例:
window.location = "https://stackoverflow.com/";
以下是关于此议题的过去文章:我该如何转到另一个网页?
其他回答
ECMAScript 6 + jQuery,85 字节
$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")
请不要杀我,这是个玩笑,这是个玩笑这是一个笑话。
这“为问题提供了答案”, 意思是它要求找到“使用jQuery”的解决方案,
Ferrybig显然需要解释笑话(还是开玩笑,
其他的答案是使用 jQuery 的attr()
会 议 日 程 和 议 程location
或window
不必要的物体。
这个答案也滥用了它, 但以一种更荒谬的方式。 它不是用它来设定位置, 而是用它来设置位置,attr()
获取设置位置的函数。
函数被命名jQueryCode
即使它没有任何jQuery, 并且号召一个函数somethingCode
特别是当事情甚至不是语言的时候。
“85字节”是指高尔夫代码。高尔夫显然不是你在高尔夫代码之外应该做的,此外,这个答案显然不是实际的高尔夫。
基本上, 畏缩。
使用 JavaScript :
方法1:
window.location.href="http://google.com";
方法2:
window.location.replace("http://google.com");
使用 j 查询 :
方法1:美元(地点)
$(location).attr('href', 'http://google.com');
方法2:可再使用功能
jQuery.fn.redirectTo = function(url){
window.location.href = url;
}
jQuery(window).redirectTo("http://google.com");
标注描述 :
window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');
杰克里:
var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window
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.
使用location.replace()
这将调整您的方向, 但不保存上一页的历史。 提交表格时最好使用此选项 。
但当你想保留自己的历史时,你必须使用location.href=//path
.
实例:
// Form with steps
document.getElementById('#next').onclick = function() {
window.location.href='/step2' // Iteration of steps;
}
// Go to next step
document.getElementById('#back').onclick = function() {
window.history.back();
}
// Finish
document.getElementById('#finish').onclick = function() {
window.location.href = '/success';
}
// On success page
window.onload = function() {
setTimeout(function() {
window.location.replace('/home'); // I can't go back to success page by pressing the back button
},3000);
}
推荐文章
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 如何在JSON中使用杰克逊更改字段名
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?