我如何将用户从一个页面转向另一个页面,使用jQuery或纯粹的JavaScript?


当前回答

下面是将代码转向另一个页面,时间为10秒。

<script>
    function Redirect()
    {
        window.location="http://www.adarshkr.com";
    }

    document.write("You will be redirected to a new page in 10 seconds.");
    setTimeout('Redirect()', 10000);
</script>

你也可以这样做,点击一个按钮使用 location.assign:

<input type="button" value="Load new document" onclick="newPage()">
<script>
    function newPage() {
        window.location.assign("http://www.adarshkr.com")
    }
</script>

其他回答

使用 JavaScript:

方法1:

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

方法2:

window.location.replace("http://google.com");

使用 jQuery:

方法1: $(位置)

$(location).attr('href', 'http://google.com');

方法2:可重新使用的功能

jQuery.fn.redirectTo = function(url){
    window.location.href = url;
}

jQuery(window).redirectTo("http://google.com");

在 PHP、HTML 或 jQuery 部分后写下下面的代码,如果在 PHP 或 HTML 部分中间,则使用 <script> 标签。

location.href = "http://google.com"

在 jQuery 中,使用 $(位置)。attr('href', url):

$(document).ready(function(){ var url = "https://www.youtube.com/watch?v=JwMKRevYa_M"; $(location).attr('href', url); // 使用此 }); <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";

因为 window.location 返回一个对象,默认设置其.href 属性。

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

将现行窗口的位置替换为新窗口。

self.location = "http://www.somewebsite.com";

设置当前窗口本身的位置。

下面是JavaScript在一定时间(3秒)后重新引导的例子:

<script> setTimeout(函数() { window.location.href = “https://www.youtube.com/”; }, 3000); </script>

使用:

function redirect(a) {
    location = a
}

求主引导他,求主引导他。

没有必要在位置后进行Href,因为它是有意义的。

警告:这个答案只是作为一个可能的解决方案提供;它显然不是最好的解决方案,因为它需要jQuery。

$(location).prop('href', 'http://stackoverflow.com')