使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
您可以使用以下方法重定向页面:
通过在头部使用元标记 -
<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/');
其他回答
您可以在 jQuery 中像这样重定向 :
$(location).attr('href', 'http://yourPage.com/');
您可以在没有jQuery 的情况下这样做 :
window.location = "http://yourdomain.com";
如果你只想要jQuery, 你可以做它喜欢:
$jq(window).attr("location","http://yourdomain.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/")
以下是一个时间延迟重定向。 您可以将延迟时间设定为任何您想要的时间 :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Your Document Title</title>
<script type="text/javascript">
function delayer(delay) {
onLoad = setTimeout('window.location.href = "http://www.google.com/"', delay);
}
</script>
</head>
<body>
<script>
delayer(8000)
</script>
<div>You will be redirected in 8 seconds!</div>
</body>
</html>
指定位置( ) :
将一条路径通过一条路径进入它 来指定一条路径...指派即使在指定路径之后, 也会给你们一个历史 。
用法:数值应传递到它中 。
例如:
location.assign("http://google.com")
位置.href
定义可以给它一个路径... 并且一旦它建立,它会重新定位到一个指定路径, 它会保存历史...
用法:值应该被指定到它中 。
例如:
location.href = "http://google.com"
位置. replace () :
如果您不想保留历史, 它会帮助您替换一条路径。 一旦您替换了一条路径, 它不会给您一个历史 。
用法:数值应传递到它中。
例如:
location.replace("http://google.com")
assign()
和href
两者相似,可以保留历史。assign
通过分配,通过传递一个价值和粗略的作品来工作。
您可以使用 JavaScript 自己实现它, 而不用 jQuery 来指定 :
window.location = "http://google.com"
location.href = "http://google.com"
使用下面的jQuery 也可以实现类似的目标。 它会做与上面完全相同的,
$(window).attr('location', "http://www.google.com");
$(location).attr('href', "http://www.google.com");
你可以很容易地理解 两者之间的区别...
这里是定位对象,
推荐文章
- 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)之间的区别是什么?