Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何重定向到其他网页?_Javascript_Jquery_Redirect - Fatal编程技术网

Javascript 如何重定向到其他网页?

Javascript 如何重定向到其他网页?,javascript,jquery,redirect,Javascript,Jquery,Redirect,如何使用jQuery或纯JavaScript将用户从一个页面重定向到另一个页面?如果您在尝试执行的操作中更具描述性,这会有所帮助。如果您正试图生成分页数据,那么在如何生成分页数据方面有一些选项。您可以为希望直接访问的每个页面生成单独的链接 <a href='/path-to-page?page=1' class='pager-link'>1</a> <a href='/path-to-page?page=2' class='pager-link'>2</

如何使用jQuery或纯JavaScript将用户从一个页面重定向到另一个页面?

如果您在尝试执行的操作中更具描述性,这会有所帮助。如果您正试图生成分页数据,那么在如何生成分页数据方面有一些选项。您可以为希望直接访问的每个页面生成单独的链接

<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>
...
我们不能简单地使用jQuery重定向 jQuery不是必需的,最好模拟HTTP重定向

窗口。位置。替换。。。比使用window.location.href更好,因为replace不会在会话历史记录中保留原始页面,这意味着用户不会陷入无休止的后退按钮失败

如果要模拟某人单击链接,请使用 location.href

如果要模拟HTTP重定向,请使用location.replace

例如:

警告:此答案仅作为可能的解决方案提供;这显然不是最好的解决方案,因为它需要jQuery。相反,我们更喜欢纯JavaScript解决方案

$location.attr'href','http://stackoverflow.com'
这适用于每个浏览器:

window.location.href = 'your_url';

这适用于jQuery:

$(window).attr("location", "http://google.fr");

您可以在不使用jQuery的情况下执行以下操作:

window.location = "http://yourdomain.com";
如果您只需要jQuery,那么您可以这样做:

$jq(window).attr("location","http://yourdomain.com");
重定向页面的标准JavaScript方法 或者更简单地说:因为窗口是全局的 如果您在这里是因为重定向时丢失HTTP_REFERER,请继续阅读:

否则忽略最后一部分

下面的部分是针对那些使用HTTP_REFERER作为众多安全措施之一的人的,尽管它不是一个很好的保护措施。如果使用的是或更低版本,则在使用任何形式的JavaScript页面重定向location.href等时,这些变量都会丢失

下面我们将为IE8&lower实现一个替代方案,这样我们就不会丢失HTTP\U REFERER。否则,您几乎总是可以简单地使用window.location.href

测试HTTP\U参考URL粘贴、会话等有助于判断请求是否合法。 注意:正如droop在评论中的链接所指出的,还有一些方法可以绕过/欺骗这些推荐人

Internet Explorer 9+和所有其他浏览器的简单跨浏览器测试解决方案回退到window.location.href

用法:重定向'anotherpage.aspx'


但如果有人想重定向回主页,那么他可以使用以下代码段

window.location = window.location.host
如果您有三种不同的环境,如开发、登台和生产,这将非常有用


您只需将这些文字放入Chrome控制台或Windows的控制台,即可浏览此窗口或window.location对象。

在您的单击功能中,只需添加:

window.location.href = "The URL where you want to redirect";
$('#id').click(function(){
    window.location.href = "http://www.google.com";
});
我还认为location.replaceURL是最好的方法,但是如果你想通知搜索引擎你的重定向,他们不会分析JavaScript代码来查看重定向,你应该在你的网站上添加rel=canonical meta标记

添加包含HTML刷新元标记的noscript部分也是一个很好的解决方案。我建议您使用它来创建重定向。它还支持InternetExplorer传递HTTP引用

没有延迟的示例代码如下所示:

<!-- 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.com/"/>
<noscript>
    <meta http-equiv="refresh" content="0;URL=https://yourdomain.com/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://yourdomain.com/";
    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 -->
$(location).attr('href', 'http://yourPage.com/');

首先要写好。您希望在应用程序中导航以获取另一个链接,而不是在应用程序中导航以获取另一个链接。代码如下:

window.location.href = "http://www.google.com";

如果您想在应用程序中导航页面,那么我也有代码,如果您愿意。

在JavaScript和jQuery中,我们可以使用以下代码将一个页面重定向到另一个页面:

window.location.href="http://google.com";
window.location.replace("page1.html");

jQuery是不需要的。您可以这样做:

window.open("URL","_self","","")
就这么简单


启动HTTP请求的最佳方法是使用document.loacation.href.replace'URL.

因此,问题是如何创建重定向页面,而不是如何重定向到网站

您只需使用JavaScript即可实现此目的。下面是一些创建动态重定向页面的小代码

<script>
    var url = window.location.search.split('url=')[1]; // Get the URL after ?url=
    if( url ) window.location.replace(url);
</script>

试试这个:

location.assign("http://www.google.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>

JavaScript提供了许多方法来检索和更改显示在浏览器地址栏中的当前URL。所有这些方法都使用Location对象,它是Window对象的一个属性。您可以创建具有当前URL的新位置对象,如下所示

var currentLocation = window.location;
URL的基本结构

Location对象也有这三种方法

分配-加载新文档。 重新加载-重新加载当前文档。 替换-将当前文档替换为新文档 您还可以使用assign和replace方法重定向到其他类似的页面

location.assign("http://www.stackoverflow.com");

location.replace("http://www.stackoverflow.com");
assign和replace的区别-replace方法和assign方法的区别在于replace从文档历史记录中删除当前文档的URL,这意味着无法使用“上一步”按钮导航回原始文档。因此,如果要加载新文档,请使用assign方法 nt提供导航回原始文档的选项

您可以使用jQuery更改location对象href属性,如下所示

$(location).attr('href',url);

因此,您可以将用户重定向到其他url。

您需要在代码中添加这一行:

$(location).attr("href","http://stackoverflow.com");
如果没有jQuery,请使用JavaScript:

window.location.replace("http://stackoverflow.com");
window.location.href("http://stackoverflow.com");
window.location.href = "/contact/";

有很多方法可以做到这一点

// 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')
使用jQuery/JavaScript方法重定向HTML页面

请尝试以下示例代码:

function YourJavaScriptFunction()
{
    var i = $('#login').val();
    if (i == 'login')
        window.location = "Login.php";
    else
        window.location = "Logout.php";
}

如果您想提供一个完整的URL作为window.location=www.google.co.in

应该只能够使用window.location进行设置

例如:


这是一篇关于这个主题的过去的帖子:

有三种主要的方法可以做到这一点

window.location.href='blaah.com';
window.location.assign('blaah.com');
而且

window.location.replace('blaah.com');
对于传统的重定向,最后一个是最好的,因为它不会保存您在搜索历史记录中被重定向之前访问的页面。但是,如果您只想用JavaScript打开一个选项卡,您可以使用上面的任何一种

编辑:窗口前缀是可选的。

在我开始之前,jQuery是一个用于DOM操作的JavaScript库。因此,您不应该将jQuery用于页面重定向

Jquery.com上的一句话:

虽然jQuery在较旧的浏览器版本中运行时可能不会出现重大问题, 我们不会在它们中主动测试jQuery,通常也不会修复bug 这可能会出现在他们身上

在这里发现:

因此,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>
当使用replace时,back按钮不会返回重定向页面,就好像它从未出现在历史记录中一样。如果希望用户能够返回重定向页面,请使用window.location.href或window.location.assign。如果您确实使用了允许用户返回重定向页面的选项,请记住,当您进入重定向页面时,它会将您重定向回。因此,在选择重定向选项时要考虑到这一点。在用户执行操作时页面仅重定向的情况下,将页面置于后退按钮历史记录中是可以的。但是如果页面自动重定向,那么您应该使用replace,这样用户就可以使用back按钮,而不必被迫返回重定向发送的页面

您还可以使用元数据运行页面重定向,如下所示

元刷新

元位置

基地劫持

可以在此页面上找到更多方法,将毫无戒心的客户端重定向到他们可能不想访问的页面,其中没有一种方法依赖于jQuery:

我还想指出,人们不喜欢被随机重定向。只有在绝对需要时才重新引导人员。如果你开始随机重定向用户,他们将永远不会再访问你的站点

下一段是假设性的:

您还可能被报告为恶意网站。如果发生这种情况,那么当人们单击指向您站点的链接时,用户浏览器可能会警告他们您的站点是恶意的。如果有人在你的网站上报告了不好的体验,搜索引擎可能会开始降低你的评级

请查看有关重定向的Google网站管理员指南:

这里有一个有趣的小页面,可以把你踢出页面

<!DOCTYPE html>
<html>
    <head>
        <title>Go Away</title>
    </head>
    <body>
        <h1>Go Away</h1>
        <script>
            setTimeout(function(){
                window.history.back();
            }, 3000);
        </script>
    </body>
</html>

如果将这两个页面示例组合在一起,您将有一个婴儿级的重新路由循环,这将保证您的用户永远不会再使用您的站点。

您可以在jQuery中重定向如下:

<!-- 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.com/"/>
<noscript>
    <meta http-equiv="refresh" content="0;URL=https://yourdomain.com/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://yourdomain.com/";
    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 -->
$(location).attr('href', 'http://yourPage.com/');
原始问题:如何使用jQuery重定向?因此答案实现了jQuery>>免费用例

要仅重定向到使用JavaScript的页面,请执行以下操作:

window.location.replace("http://stackoverflow.com");
window.location.href("http://stackoverflow.com");
window.location.href = "/contact/";
或者,如果您需要延迟:

setTimeout(function () {
  window.location.href = "/contact/";
}, 2000);   // Time in milliseconds
jQuery允许您轻松地从网页中选择元素。您可以在页面上找到所需的任何内容,然后使用jQuery添加特效、对用户操作作出反应,或者在所选元素的内部或外部显示和隐藏内容。所有这些任务都是从了解开始的

想象一下,有人编写了一个包含10000行代码的脚本/插件。使用jQuery,您只需一两行代码即可连接到此代码。

Javascript:

window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');
Jquery:

var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window
ECMAScript 6+jQuery,85字节 请不要杀我,这是个玩笑。这是个笑话。这是个笑话

这确实为这个问题提供了答案,从某种意义上说,它要求使用jQuery来解决问题,在本例中,jQuery需要以某种方式将其强制到等式中

Ferrybig显然需要解释这个笑话,但仍然在开玩笑,我确信审查表上的选项有限,因此无需进一步讨论:

其他答案是不必要地在位置或窗口对象上使用jQuery的attr

这个答案也滥用了它,但以一种更荒谬的方式。不是使用它来设置位置,而是使用attr来检索设置位置的函数

函数 ion被命名为jQueryCode,尽管它没有jQuery,调用函数somethingCode非常可怕,特别是当something甚至不是一种语言时

85字节是对代码的引用。高尔夫显然不是代码高尔夫之外的事情,而且这个答案显然不是真正的高尔夫

基本上是畏缩。

使用JavaScript: 方法1: 方法2: 使用jQuery: 方法1:$location 方法2:可重用函数 基本上,jQuery只是一个JavaScript框架,在本例中,为了执行重定向等操作,您可以使用纯JavaScript,因此在这种情况下,您有3个使用普通JavaScript的选项:

window.location.replace("http://stackoverflow.com");
window.location.href("http://stackoverflow.com");
window.location.href = "/contact/";
1使用位置替换,这将替换页面的当前历史记录,意味着无法使用“后退”按钮返回到原始页面

window.location.replace("http://stackoverflow.com");
2使用location assign,这将为您保留历史记录,使用back按钮,您可以返回到原始页面:

window.location.assign("http://stackoverflow.com");
3我建议使用上述方法之一,但这可能是使用纯JavaScript的第三种选择:

window.location.href="http://stackoverflow.com";
您也可以在jQuery中编写一个函数来处理它,但不推荐这样做,因为它只是一行纯JavaScript函数,如果您已经在窗口范围内,也可以在没有窗口的情况下使用上述所有函数,例如window.location.replacehttp://stackoverflow.com; 可能是location.replacehttp://stackoverflow.com;

我还将在下图中显示它们:


注意:类似于HTTP redirect for replace的行为意味着它不会在浏览器的历史记录中创建条目。我在5年前看到您的答案后创建了此项,如果您仅显示一个页面以进行重定向,location.replace可能更适合从历史记录中排除具有重定向的页面。但是为什么不首先在服务器端执行重定向呢?
<!DOCTYPE html>
<html>
    <head>
        <title>Go Away</title>
    </head>
    <body>
        <h1>Go Away</h1>
        <script>
            setTimeout(function(){
                window.history.back();
            }, 3000);
        </script>
    </body>
</html>
$(location).attr('href', 'http://yourPage.com/');
window.location.href = "/contact/";
setTimeout(function () {
  window.location.href = "/contact/";
}, 2000);   // Time in milliseconds
$('a,img').on('click',function(e){
  e.preventDefault();
  $(this).animate({
    opacity: 0 //Put some CSS animation here
  }, 500);
  setTimeout(function(){
    // OK, finished jQuery staff, let's go redirect
    window.location.href = "/contact/";
  },500);
});
window.location.href='www.your_url.com';
window.top.location.href='www.your_url.com';
window.location.replace('www.your_url.com');
var url='www.your_url.com';
$(location).attr('href',url);
$(location).prop('href',url);//instead of location you can use window
$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")
window.location.href="http://google.com";
window.location.replace("http://google.com");
$(location).attr('href', 'http://google.com');
jQuery.fn.redirectTo = function(url){
    window.location.href = url;
}

jQuery(window).redirectTo("http://google.com");
window.location.replace("http://stackoverflow.com");
window.location.assign("http://stackoverflow.com");
window.location.href="http://stackoverflow.com";