使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
我们不能简单地使用 jQuery 重置
j/ 查询没有必要,window.location.replace(...)
最好模拟 HTTP 重定向 。
window.location.replace(...)
优于使用window.location.href
,因为replace()
用户不会陷入永无止境的后端故障中。
如果您想要模拟某人点击链接, 请使用location.href
如果您想要模拟 HTTP 重定向,请使用location.replace
例如:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
其他回答
使用 jQuery 函数 :
$.extend({
redirectPost: function(location, args) {
var form = '';
$.each(args, function(key, value) {
form += '<input type="hidden" name="' + key + '" value="' + value + '">';
});
$('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
}
});
在你的代码中,你使用它这样:
$.redirectPost("addPhotos.php", {pimreference: $("#pimreference").val(), tag: $("#tag").val()});
每个浏览器都使用此功能 :
window.location.href = 'your_url';
我已经使用 JavaScript 的函数重定向() 。 它正在工作 。
<script type="text/javascript">
$(function () {
//It's similar to HTTP redirect
window.location.replace("http://www.Technomark.in");
//It's similar to clicking on a link
window.location.href = "Http://www.Technomark.in";
})
</script>
使用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);
}
指定位置( ) :
将一条路径通过一条路径进入它 来指定一条路径...指派即使在指定路径之后, 也会给你们一个历史 。
用法:数值应传递到它中 。
例如:
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)之间的区别是什么?