我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?
当前回答
在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");
其他回答
如果你正在尝试生成包数据,有几个选项,你如何做到这一点,你可以生成单独的链接,每个页面你想能够直接到。
<a href='/path-to-page?page=1' class='pager-link'>1</a>
<a href='/path-to-page?page=2' class='pager-link'>2</a>
<span class='pager-link current-page'>3</a>
...
请注意,示例中的当前页面在代码和CSS中以不同的方式处理。
如果您希望通过 AJAX 更改已支付的数据,这就是 jQuery 会进入的地方. 您所做的就是将一个点击处理器添加到相应不同页面的每个 anchor 标签. 这个点击处理器会引用一些 jQuery 代码,并通过 AJAX 获取下一个页面并更新新数据的表。
$(document).ready( function() {
$('a.pager-link').click( function() {
var page = $(this).attr('href').split(/\?/)[1];
$.ajax({
type: 'POST',
url: '/path-to-service',
data: page,
success: function(content) {
$('#myTable').html(content); // replace
}
});
return false; // to stop link
});
});
您可以在 jQuery 中以以下方式重定向:
$(location).attr('href', 'http://yourPage.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')
我也认为 location.replace(URL)是最好的方式,但如果你想通知搜索引擎你的重新引导(他们不分析JavaScript代码查看重新引导),你应该添加 rel="canonical" meta 标签到你的网站。
添加一个Noscript 部分,其中有一个 HTML 更新 meta 标签,也是一个很好的解决方案. 我建议您使用这个 JavaScript 重定向工具来创建重定向。
样品代码无延迟看起来如下:
<!-- Place this snippet right after opening the head tag to make it work properly -->
<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->
<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://yourdomain.example/"/>
<noscript>
<meta http-equiv="refresh" content="0;URL=https://yourdomain.example/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
var url = "https://yourdomain.example/";
if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
{
document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
var referLink = document.createElement("a");
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
}
else { window.location.replace(url); } // All other browsers
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->
JavaScript 为您提供了许多方法,以获取和更改浏览器的地址栏中显示的当前 URL. 所有这些方法都使用位置对象,这是窗口对象的属性. 您可以创建一个新的位置对象,具有当前 URL。
var currentLocation = window.location;
一个URL的基本结构
<protocol>//<hostname>:<port>/<pathname><search><hash>
此分類上一篇
协议 - 指定协议名称用于访问互联网上的资源. (HTTP(没有SSL)或HTTPS(有SSL))主机名称 - 主机名称指定主机拥有资源. 例如, www.stackoverflow.com. 服务器提供服务,使用主机名称。
使用这些位置对象属性,您可以访问所有这些 URL 组件
现在,如果您想要更改一个页面或将用户转向另一个页面,您可以使用位置对象的href属性,如此。
您可以使用位置对象的 href 属性。
window.location.href = "http://www.stackoverflow.com";
位置对象也有这些三种方法
location.assign("http://www.stackoverflow.com");
location.replace("http://www.stackoverflow.com");
$(location).attr('href',url);
推荐文章
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 如何在JSON中使用杰克逊更改字段名
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法