使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
# HTML 页面使用 jQuery/JavaScript 方法重定向
尝试此示例代码 :
function YourJavaScriptFunction()
{
var i = $('#login').val();
if (i == 'login')
window.location = "Login.php";
else
window.location = "Logout.php";
}
如果您想要给完整 URL 提供完整 URLwindow.location = "www.google.co.in";
.
其他回答
这样做有许多方法。
// 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')
jj 用于重定向页面或 URL 的查询代码
第一路
这是用于重定向页面的 jQuery 代码 。 既然, 我已经把这个代码放在$( document). ready () 函数上, 一旦页面加载, 它将会立即执行 。
var url = "http://stackoverflow.com";
$(location).attr('href',url);
您甚至可以直接将一个 URL 直接传送到attr()方法,而不是使用变量。
第二路
window.location.href="http://stackoverflow.com";
您也可以使用这样的代码(两者在内部相同):
window.location="http://stackoverflow.com";
如果您对窗口. 位置和window.location.href
,然后你可以看到后一个设置href
明确表明财产,而前者默示地这样做。window.location
返回一个对象,该对象默认设置它.href
财产。
第三路
使用 JavaScript (JavaScript)replace()
方法方法的计算方法的计算方法window.location
对象。您可以将新的 URL 传送到replace()
方法, 它将模拟 HTTP 重定向。 顺便说一下, 记住window.location.replace()
方法不会在会话历史中放置原始页面, 这可能影响到后按钮的行为。 有时, 它会是你想要的, 所以要小心使用它 。
// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");
第四路
缩略location.assign()
方法在浏览器窗口中装入新文档。
window.location.assign("http://stackoverflow.com");
和(或)assign()
和replace()
方法为location.replace()
从文档历史中删除当前 URL 的方法,因此无法返回原始文档。您不能使用浏览器。后退按钮。如果您想要避免这种情况,应该使用location.assign()
方法,因为它在浏览器中装入一个新的文档。
第五路
类似attr()方法 (在 jQuery 1. 6 引入后)
var url = "http://stackoverflow.com";
$(location).prop('href', url);
在 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/")
如果您喜欢使用纯 JavaScript 我意识到使用document.location.href = "https://example.com"
或window.location.href = "https://example.com"
导致 Firefox 的兼容问题。 相反, 尝试使用 :
location.href = "https://example.com";
location.replace("http://example.com");
我的情况解决了问题 祝你好运!
单页应用在同一申请路线内
window.location.pathname = '/stack';
JavaScript :
location.href = "http://stack.com";
window.location = "http://stack.com";
jj 查询 :
$(location).attr('href', "http://www.stack.com");
$(window).attr('location', "http://www.stack.com");
角形 4
import { Router } from '@angular/router';
export class NavtabComponent{
constructor(private router: Router) {
}
this.router.navigate(['bookings/taxi']);
}
推荐文章
- 一元加/数字(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?