使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
标准“ vanilla” JavaScript 重定向页面的方法
window.location.href = 'newPage.html';
或更简单: (自window
全球)
location.href = 'newPage.html';
如果你在这里,因为你损失HTTP_REFERERER 重定向时, 继续读取 :
(否则忽略此最后部分)
下一节针对的是那些使用HTTP_REFERER
作为许多安全措施之一的安全措施(尽管这不是一个伟大的保护措施)。因特网探索者 8使用任何形式的 JavaScript 页面重定向( 位置.href 等) 时, 这些变量会丢失 。
我们将在下面实施一个替代方案:IE8 更低这样我们就不会失去 HTTP_REFERER。 否则,你几乎总是可以简单地使用 HTTP_REFERERER。window.location.href
.
测试HTTP_REFERER
(URL 粘贴、 会话等)能够帮助判断请求是否合法 。(注:正如Droop在评论中指出的,也有一些办法可以对这些裁判员进行变通/补充)
简单的交叉浏览器测试解决方案( 返回窗口. place.href 用于 Internet Explorer 9+ 和所有其他浏览器)
用法 :redirect('anotherpage.aspx');
function redirect (url) {
var ua = navigator.userAgent.toLowerCase(),
isIE = ua.indexOf('msie') !== -1,
version = parseInt(ua.substr(4, 2), 10);
// Internet Explorer 8 and lower
if (isIE && version < 9) {
var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();
}
// All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
else {
window.location.href = url;
}
}
其他回答
标准“ vanilla” JavaScript 重定向页面的方法
window.location.href = 'newPage.html';
或更简单: (自window
全球)
location.href = 'newPage.html';
如果你在这里,因为你损失HTTP_REFERERER 重定向时, 继续读取 :
(否则忽略此最后部分)
下一节针对的是那些使用HTTP_REFERER
作为许多安全措施之一的安全措施(尽管这不是一个伟大的保护措施)。因特网探索者 8使用任何形式的 JavaScript 页面重定向( 位置.href 等) 时, 这些变量会丢失 。
我们将在下面实施一个替代方案:IE8 更低这样我们就不会失去 HTTP_REFERER。 否则,你几乎总是可以简单地使用 HTTP_REFERERER。window.location.href
.
测试HTTP_REFERER
(URL 粘贴、 会话等)能够帮助判断请求是否合法 。(注:正如Droop在评论中指出的,也有一些办法可以对这些裁判员进行变通/补充)
简单的交叉浏览器测试解决方案( 返回窗口. place.href 用于 Internet Explorer 9+ 和所有其他浏览器)
用法 :redirect('anotherpage.aspx');
function redirect (url) {
var ua = navigator.userAgent.toLowerCase(),
isIE = ua.indexOf('msie') !== -1,
version = parseInt(ua.substr(4, 2), 10);
// Internet Explorer 8 and lower
if (isIE && version < 9) {
var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();
}
// All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
else {
window.location.href = url;
}
}
这样做有许多方法。
// 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");
基本上jj 查询只是一个JavaScript 贾斯克里普特和做一些诸如转向在此情况下, 您可以使用纯 JavaScript, 这样您就有三个选择, 使用 vanilla JavaScript :
(1) 使用地点替换,这将取代页面的当前历史,这意味着无法使用后退按钮返回原始页面。
window.location.replace("http://stackoverflow.com");
2) 使用地点指派,这将保留您的历史,如果使用后按钮,您可以回到原始页面:
window.location.assign("http://stackoverflow.com");
3) 我建议使用其中一种方法, 但这可能是使用纯JavaScript的第三个选择:
window.location.href="http://stackoverflow.com";
您也可以在 jQuery 中写入一个函数来处理它, 但由于它只是一行纯 JavaScript 函数, 您也可以在 jQuery 中写入一个函数来处理它, 但由于它只是一行纯 JavaScript 函数, 您也可以使用所有上面没有窗口的函数, 如果您已经进入窗口范围, 例如window.location.replace("http://stackoverflow.com");
可以是location.replace("http://stackoverflow.com");
我还在下面的图像上展示了所有这些照片:
在 JavaScript 和 jQuery 中, 我们使用以下代码重定向页面 :
window.location.href="http://google.com";
window.location.replace("page1.html");
但您可以在 jQuery 中设定函数, 以重定向页面 :
jQuery.fn.redirect=function(url)
{
window.location.href=url;
}
调用此函数 :
jQuery(window).redirect("http://stackoverflow.com/")
推荐文章
- 一元加/数字(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?