使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
这很容易执行。 您可以使用 :
window.location.href = "http://www.example.com/";
这将记住上一页的历史。 这样可以点击浏览器的后键返回历史 。
或者:
window.location.replace("http://www.example.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()});
JavaScript 非常广泛。 如果您想要跳转到另一页, 您有三个选项 。
window.location.href='otherpage.com';
window.location.assign('otherpage.com');
//and...
window.location.replace('otherpage.com');
当您想要移动到另一页时, 如果这是您的要求, 您可以使用其中的任何内容 。 但是所有三种选择都局限于不同的情况 。 请按照您的要求明智地选择 。
如果您有兴趣了解更多有关这个概念的知识,可以更深入地了解。
window.location.href; // Returns the href (URL) of the current page
window.location.hostname; // Returns the domain name of the web host
window.location.pathname; // Returns the path and filename of the current page
window.location.protocol; // Returns the web protocol used (http: or https:)
window.location.assign; // Loads a new document
window.location.replace; // RReplace the current location with new one.
如果您喜欢使用纯 JavaScript 我意识到使用document.location.href = "https://example.com"
或window.location.href = "https://example.com"
导致 Firefox 的兼容问题。 相反, 尝试使用 :
location.href = "https://example.com";
location.replace("http://example.com");
我的情况解决了问题 祝你好运!
内jj 查询,使用$(location).attr('href', url)
:
$(document).ready(function(){
var url = "https://www.youtube.com/watch?v=JwMKRevYa_M";
$(location).attr('href', url); // Using this
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
内原始 JavaScript 贾斯克里普特实现这一点有若干途径:
window.location.href="https://www.youtube.com/watch?v=JwMKRevYa_M";
- 明确设定 href 属性。
window.location = "http://www.GameOfThrones.com";
- 是否暗含它 因为窗口。 位置返回一个对象, 默认设置它的. href 属性 。
window.location.replace("http://www.stackoverflow.com");
- 将当前窗口的位置替换为新窗口。
self.location = "http://www.somewebsite.com";
- 设置当前窗口本身的位置。
以下是JavaScript在一定时间(3秒)后重新定向的例子:
<script>
setTimeout(function() {
window.location.href = "https://www.youtube.com/";
}, 3000);
</script>
在我开始之前, jQuery 是用于 DOM 操作的 JavaScript 库。 所以您不应该使用 jQuery 重定向页面 。
Jquery.com的一段引文:
虽然jQuery在旧浏览器版本中可能运行没有大问题,但我们并不积极测试jQuery在其中的位置,而且通常不修补其中可能出现的错误。
发现于此:https://jquery.com/browser-support/
所以jQuery并不是一个终极和万能的解决方案 解决逆向兼容性。
以下使用原始 JavaScript 的解决方案在所有浏览器中都起作用, 并且已经相当长的时间达到了标准, 所以您不需要任何库来支持交叉浏览器 。
此页面将重定向到谷歌谷歌3000毫秒后
<!DOCTYPE html>
<html>
<head>
<title>example</title>
</head>
<body>
<p>You will be redirected to google shortly.</p>
<script>
setTimeout(function(){
window.location.href="http://www.google.com"; // The URL that will be redirected too.
}, 3000); // The bigger the number the longer the delay.
</script>
</body>
</html>
不同的备选办法如下:
window.location.href="url"; // Simulates normal navigation to a new page
window.location.replace("url"); // Removes current URL from history and replaces it with a new URL
window.location.assign("url"); // Adds new URL to the history stack and redirects to the new URL
window.history.back(); // Simulates a back button click
window.history.go(-1); // Simulates a back button click
window.history.back(-1); // Simulates a back button click
window.navigate("page.html"); // Same as window.location="url"
使用替换时, 后端按钮将不再返回重置页面, 仿佛它从来没有在历史中出现过。 如果您想要用户能够返回重置页面, 请使用window.location.href
或window.location.assign
。如果您确实使用让用户返回重定向页面的选项,请记住,当您输入重定向页面时,它会将您重新定向。因此,在选择重定向选项时会考虑到这一点。如果该页面仅在用户采取行动时才被重定向,那么在后按钮历史中拥有页面就没事了。但如果页面自动重定向,那么您应该使用替换,这样用户就可以使用后端按钮,而不必被迫返回重定向发送的页面。
您也可以使用元数据运行页面重定向 。
META 刷新
<meta http-equiv="refresh" content="0;url=http://evil.example/" />
METTA 地点
<meta http-equiv="location" content="URL=http://evil.example" />
BASE 劫持
<base href="http://evil.example/" />
有许多方法可以将您未预料到的客户端转到他们可能不希望访问的页面上(其中没有一个是依赖jQuery):
https://code.google.com/p/html5security/wiki/RedirectionMethods
我还要指出,人们不喜欢随机调整方向。 只有在绝对需要的时候才会调整方向。 如果您开始随机调整方向, 他们就不会再去你的网站了 。
下一段是假设性的:
您也可以被报告为恶意网站。如果发生这种情况,当人们点击与您网站的链接时,用户浏览器可能会警告他们您的网站是恶意的。同样可能发生的是搜索引擎可能会开始降低您的评级,如果有人在您的网站上报告有不良经历的话。
请审查Google网站技术主管指南关于改方向的内容:https://support.google.com/webmasters/answer/2721217?hl=en&ref_topic=6001971
这是一个有趣的小页 踢你出页面。
<!DOCTYPE html>
<html>
<head>
<title>Go Away</title>
</head>
<body>
<h1>Go Away</h1>
<script>
setTimeout(function(){
window.history.back();
}, 3000);
</script>
</body>
</html>
如果您将两个页面示例合并在一起, 你会有一个新生的路线改变循环, 这将保证您的用户将永远不再想要使用您的网站 。