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.replace(…)
优于使用
window.location.href
,因为
replace()
不会在会话历史记录中保留原始页面,这意味着用户不会陷入无休止的后退按钮失败

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

function redirect (url) {
    var ua        = navigator.userAgent.toLowerCase(),
        isIE      = ua.indexOf('msie') !== -1,
        version   = parseInt(ua.substr(4, 2), 10);

    // Internet Explorer 8 and lower
    if (isIE && version < 9) {
        var link = document.createElement('a');
        link.href = url;
        document.body.appendChild(link);
        link.click();
    }

    // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
    else { 
        window.location.href = url; 
    }
}
如果要模拟HTTP重定向,请使用
location.replace

function redirect (url) {
    var ua        = navigator.userAgent.toLowerCase(),
        isIE      = ua.indexOf('msie') !== -1,
        version   = parseInt(ua.substr(4, 2), 10);

    // Internet Explorer 8 and lower
    if (isIE && version < 9) {
        var link = document.createElement('a');
        link.href = url;
        document.body.appendChild(link);
        link.click();
    }

    // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
    else { 
        window.location.href = url; 
    }
}
例如:

// 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";
window.location = "https://stackoverflow.com/";

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

$(位置).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\u REFERER,请继续阅读:

// 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";
window.location = "https://stackoverflow.com/";
(否则忽略最后一部分)


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

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

对HTTP_REFERER(URL粘贴、会话等)进行测试有助于判断请求是否合法。 (注意:如droop在评论中的链接所述,还有一些方法可以绕过/欺骗这些推荐人)


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

用法:
重定向('anotherpage.aspx')

function redirect (url) {
    var ua        = navigator.userAgent.toLowerCase(),
        isIE      = ua.indexOf('msie') !== -1,
        version   = parseInt(ua.substr(4, 2), 10);

    // Internet Explorer 8 and lower
    if (isIE && version < 9) {
        var link = document.createElement('a');
        link.href = url;
        document.body.appendChild(link);
        link.click();
    }

    // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
    else { 
        window.location.href = url; 
    }
}
函数重定向(url){
var ua=navigator.userAgent.toLowerCase(),
isIE=ua.indexOf('msie')!=-1,
version=parseInt(ua.substr(4,2,10));
//Internet Explorer 8及更低版本
如果(isIE&&版本<9){
var link=document.createElement('a');
link.href=url;
document.body.appendChild(链接);
link.click();
}
//所有其他浏览器都可以使用标准的window.location.href(它们不会像InternetExplorer8和更低版本那样丢失HTTP\uReferer)
否则{
window.location.href=url;
}
}

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

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.replace(URL)
是最好的方法,但是如果你想通知搜索引擎你的重定向(他们不分析JavaScript代码来查看重定向),你应该在你的网站上添加
rel=“canonical”
元标记

添加包含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/');

变量url=”https://yourdomain.com/";
if(typeof IE_fix!=“undefined”)//IE8和更低版本的补丁传递http引用
{
document.write(“重定向…”);//不要删除此行,否则appendChild()将失败,因为它是在document.onload之前调用的,以使重定向尽可能快。没有人会看到此文本,这只是一个技术修复。
var referelink=document.createElement(“a”);
referelink.href=url;
document.body.appendChild(referelink);
referelink.click();
}
else{window.location.replace(url);}//所有其他浏览器

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

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>

您的文档标题
功能延迟器(延迟){
onLoad=setTimeout('window.location.href='http://www.google.com/“,延迟);
}
延迟器(8000)
您将在8秒内被重定向!

JavaScript为您提供了许多方法来检索和更改当前URL,其中
var currentLocation = window.location;
<protocol>//<hostname>:<port>/<pathname><search><hash>
window.location.href = "http://www.stackoverflow.com";
location.assign("http://www.stackoverflow.com");

location.replace("http://www.stackoverflow.com");
$(location).attr('href',url);
$(location).attr("href","http://stackoverflow.com");
window.location.replace("http://stackoverflow.com");
window.location.href("http://stackoverflow.com");
// 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')
function YourJavaScriptFunction()
{
    var i = $('#login').val();
    if (i == 'login')
        window.location = "Login.php";
    else
        window.location = "Logout.php";
}
window.location = "https://stackoverflow.com/";
window.location.href='blaah.com';
window.location.assign('blaah.com');
window.location.replace('blaah.com');
<!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"
<meta http-equiv="refresh" content="0;url=http://evil.com/" />
<meta http-equiv="location" content="URL=http://evil.com" />
<base href="http://evil.com/" />
<!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";