我如何将用户从一个页面转向另一个页面,使用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>
...

请注意,示例中的当前页面在代码和CSS中以不同的方式处理。

如果您希望通过 AJAX 更改已支付的数据,这就是 jQuery 会进入的地方. 您所做的就是将一个点击处理器添加到相应不同页面的每个 anchor 标签. 这个点击处理器会引用一些 jQuery 代码,并通过 AJAX 获取下一个页面并更新新数据的表。

$(document).ready( function() {
    $('a.pager-link').click( function() {
        var page = $(this).attr('href').split(/\?/)[1];
        $.ajax({
            type: 'POST',
            url: '/path-to-service',
            data: page,
            success: function(content) {
               $('#myTable').html(content);  // replace
            }
        });
        return false; // to stop link
    });
});

一个不只是使用jQuery重定向

jQuery 不需要, window.location.replace(...) 最好是模拟一个 HTTP 重定向。

window.location.replace(...) 比使用 window.location.href 更好,因为 replace() 不保留起源页面在会话历史中,这意味着用户不会陷入无止境后按钮故障。

如果你想模拟某人点击链接,使用 location.href

如果您想模拟一个 HTTP 重定向,请使用 location.replace。

例如:

// 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";
var url = 'asdf.html';
window.location.href = url;

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

$(location).prop('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如何重新引导页面

window.location.href = 'newPage.html';

location.href = 'newPage.html';


以下部分适用于使用 HTTP_REFERER 作为许多安全措施之一(尽管这不是一个很好的保护措施)。如果您使用 Internet Explorer 8 或更低,这些变量在使用任何形式的 JavaScript 页面重定向(location.href 等)时会失去。

下面我们将实施一个替代 IE8 和更低,以免失去 HTTP_REFERER. 否则,你几乎总是可以简单地使用 window.location.href。

对 HTTP_REFERER 进行测试(URL 粘贴、会话等)可以帮助您知道请求是否合法(注:在评论中提到 drop 的链接中,还有一些方法可以工作周围 / 扫描这些引用器)


简单的跨浏览器测试解决方案(下载到 window.location.href for Internet Explorer 9+ 和所有其他浏览器)

使用:重定向(“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; 
    }
}

但是,如果有人想重新引导到主页,那么他可以使用下面的剪辑。

window.location = window.location.host

如果你有三种不同的环境,如发展、排行和生产,这将是有用的。

您可以通过在 Chrome 控制台或 Firebug 的控制台中放置这些单词来探索此窗口或 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" meta 标签到你的网站。

添加一个Noscript 部分,其中有一个 HTML 更新 meta 标签,也是一个很好的解决方案. 我建议您使用这个 JavaScript 重定向工具来创建重定向。

样品代码无延迟看起来如下:

<!-- 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.example/"/>
<noscript>
    <meta http-equiv="refresh" content="0;URL=https://yourdomain.example/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://yourdomain.example/";
    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 -->

首先写得正确. 你想在一个应用程序内导航另一个链接从你的应用程序另一个链接. 这里是代码:

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

如果你想在你的应用程序内浏览页面,那么我也有代码,如果你愿意。

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

location.href = "http://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’)。

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

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

http://www.mywebsite.com/redirect?url=http://stackoverflow.com

如果你去那个链接,它会自动将你转向stackoverflow.com。

这就是你用JavaScript创建一个简单的重定向页面的方式。

我添加了 window.location.replace 在我的代码,因为我认为它适合一个重定向页面,但是,你必须知道,当使用 window.location.replace 和你得到重定向,当你按下后按钮在你的浏览器它不会回到重定向页面,它会回到页面之前,看看这个小演示事物。

例子:

因此,如果这符合您的需求,那么一切都应该顺利。 如果您想将重定向页面在浏览器历史中取代这个

if( url ) window.location.replace(url);

if( url ) window.location.href = url;

试试这:

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>

您可以使用它如下代码,在那里 getRequestToForwardPage 是请求地图(URL)。

function savePopUp(){
    $.blockUI();
    $.ajax({
        url:"GuestHouseProcessor?roomType="+$("#roomType").val(),
        data: $("#popForm").serialize(),
        dataType: "json",
        error: (function() {
            alert("Server Error");
            $.unblockUI();
    }),
    success: function(map) {
        $("#layer1").hide();
        $.unblockUI();
        window.location = "getRequestToForwardPage";
    }
});

这就是申请的相同背景。

如果您只想使用 jQuery 特定的代码,那么以下代码可以帮助:

 $(location).attr('href',"http://www.google.com");
 $jq(window).attr("location","http://www.google.com");
 $(location).prop('href',"http://www.google.com"); 

JavaScript 为您提供了许多方法,以获取和更改浏览器的地址栏中显示的当前 URL. 所有这些方法都使用位置对象,这是窗口对象的属性. 您可以创建一个新的位置对象,具有当前 URL。

var currentLocation = window.location;

一个URL的基本结构

<protocol>//<hostname>:<port>/<pathname><search><hash>

此分類上一篇

协议 - 指定协议名称用于访问互联网上的资源. (HTTP(没有SSL)或HTTPS(有SSL))主机名称 - 主机名称指定主机拥有资源. 例如, www.stackoverflow.com. 服务器提供服务,使用主机名称。

使用这些位置对象属性,您可以访问所有这些 URL 组件

现在,如果您想要更改一个页面或将用户转向另一个页面,您可以使用位置对象的href属性,如此。

您可以使用位置对象的 href 属性。

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");

如果您没有 jQuery,请使用 JavaScript:

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')
<script type="text/javascript">
var url = "https://yourdomain.com";

// IE8 and lower fix
if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
{
    var referLink = document.createElement("a");
    referLink.href = url;
    document.body.appendChild(referLink);
    referLink.click();
}

// All other browsers
else { window.location.replace(url); }
</script>

使用 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.replace('yourPage.aspx');   
   // If you're on root and redirection page is also on the root

   window.location.replace(window.location.host + '/subDirectory/yourPage.aspx');

   // If you're in sub directory and redirection page is also in some other sub directory.

您可以使用 window.location 设置。

例子:

window.location = "https://stackoverflow.com/";

此分類上一篇: 我如何轉向另一個網站?

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

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

和...

window.location.replace('blaah.com');

最后一个是最好的,对于一个传统的重定向,因为它不会保存你去的页面,然后在你的搜索历史中重定向。

EDIT:窗口预定是可选的。

此分類上一篇: https://jquery.com/browser-support/

<!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 数据进行页面重定向如下。

梅塔新鲜

<meta http-equiv="refresh" content="0;url=http://evil.example/" />

地图位置

<meta http-equiv="location" content="URL=http://evil.example" />

<base href="http://evil.example/" />

在此页面上可以找到许多其他方法将您的无疑客户端重新引导到他们可能不想去的页面(其中没有一个依赖于jQuery):

https://code.google.com/p/html5security/wiki/Redirection方法

您也可能被报告为恶意网站. 如果发生的话,当人们点击到您的网站的链接时,用户浏览器可能会警告他们您的网站是恶意的。

<!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 中以以下方式重定向:

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

下面是将代码转向另一个页面,时间为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>

使用 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()});

使用 jQuery/JavaScript 重新引导用户

使用 jQuery 或 JavaScript 的位置对象,我们可以将用户转向另一个网页。

在 jQuery

将用户从一页转向另一页的代码是:

var url = 'http://www.example.com';
$(location).attr('href', url);

在JavaScript

将用户从一页转向另一页的代码是:

var url = 'http://www.example.com';
window.location.href = url;

var url = 'http://www.example.com';
window.location = url;

原始问题:“如何使用 jQuery 重新引导?”,因此答案实施 jQuery >> 补充使用案例。


要重定向到使用JavaScript的页面:

window.location.href = "/contact/";

如果您需要延迟:

setTimeout(function () {
  window.location.href = "/contact/";
}, 2000);   // Time in milliseconds

jQuery 允许您轻松地从 Web 页面中选择元素. 您可以在页面上找到您想要的任何东西,然后使用 jQuery 添加特殊效果,对用户行动作出反应,或显示和隐藏您选择的元素内部或外部的内容。

$('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);
});

想象一下,有人写了一张10万行代码的脚本/插件,使用jQuery,你可以用一行或两行连接到这个代码。

我只是用另一个更新的jQuery方法更新这个荒谬:

var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);

JavaScript 的内容:

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

在JavaScript中,您可以通过使用以下方式重新引导到一个特定的页面:

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

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

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

使用 jQuery:

$(window).attr("location","http://www.test.com");

在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/")

jQuery 代码重新引导页面或 URL

您甚至可以直接将 URL 转移到 attr() 方法,而不是使用变量。

第二条路

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

你也可以编码如下(两者都是内部相同的):

window.location="http://stackoverflow.com";

第三种方式

第四条路

Location.assign() 方法将新文档上传到浏览器窗口中。

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

第五条路

如 attr() 方法( jQuery 1.6 引入后)

var url = “http://stackoverflow.com”; $(位置)。prop('href', url);

如何从客户端侧进行重定向:

<!DOCTYPE html> <html> <head> <title>JavaScript and jQuery example to redirect a page or URL </title> </head> <body> <div id="redirect"> <h2>Redirect to another page</h2> </div> <script src="scripts/jquery-1.6.2.min.js"></script> <script> // JavaScript code to redirect a URL window.location.r

ECMAScript 6 + jQuery,85位元

$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")

请不要杀了我,这是一个笑话,这是一个笑话,这是一个笑话。

这“提供了一个答案”,在这个意义上,它要求一个解决方案“使用jQuery”,这在这种情况下意味着以某种方式强迫它进入方程式。

Ferrybig 显然需要解释的笑话(仍然开玩笑,我确信在评论表格上有有限的选项),所以没有进一步的 ado:

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

这个答案也滥用它,但更荒谬的方式. 而不是使用它来设置位置,它使用 attr() 恢复一个函数,设置位置。

“85比特”是指代码高尔夫,高尔夫显然不是你应该在代码高尔夫以外做的事情,而且这个答案显然不是真正的高尔夫。

基本上是克里斯蒂安。

我已经使用JavaScript的重定向功能(),它工作了。

<script type="text/javascript">
    $(function () {
        //It's similar to HTTP redirect
        window.location.replace("http://www.Technomark.in");

        //It's similar to clicking on a link
        window.location.href = "Http://www.Technomark.in";
    })
</script>

你可以使用:

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

替代() 方法不会在会议历史中保存起源页面,因此用户无法使用后按钮返回并重新转向。

但是,如果你想要一个与点击链接相同的效果,你应该去:

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

在此情况下,浏览器返回按钮将是活跃的。

这是很容易实现的,你可以使用:

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

这将记住前一页的历史,所以可以通过点击浏览器的背面按钮返回。

或:

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

此方法不记得前一页的历史,此情况下后按钮将被禁用。

您可以使用下面的方法重新引导页面:

使用头部中的 meta 标签 - <meta http-equiv="refresh" content="0;url=http://your-page-url.com" />. 请注意,内容="0;... 使用了几秒钟后你需要重新引导页面 使用 JavaScript: window.location.href = "http://your-page-url.com"; 使用 jQuery: $(location).attr('href', 'http://yourPage.com/');

使用:

function redirect(a) {
    location = a
}

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

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

在 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>

<script type="text/javascript">
    if(window.location.href === "http://stackoverflow.com") {      
         window.location.replace("https://www.google.co.in/");
       }
</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");

基本上,jQuery只是一个JavaScript框架,在这种情况下,你只能使用纯粹的JavaScript,所以在这种情况下,你有3个选项使用VanillaJavaScript:

使用位置替换,这将取代当前页面历史,这意味着无法使用后按钮返回原始页面。

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

2) 使用位置指定,这将为您保存历史,并使用返回按钮,您可以返回原始页面:

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

3)我建议使用上述方式之一,但这可能是使用纯粹的JavaScript的第三个选项:

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

您也可以在 jQuery 中编写一个函数来处理它,但不推荐,因为它只是一个线纯的JavaScript函数,如果您已经在窗口范围内,也可以使用上面的所有函数没有窗口,例如 window.location.replace(“http://stackoverflow.com”);可以是 location.replace(“http://stackoverflow.com”);

我在下面的图像中展示了所有这些:

此分類上一篇

我只是添加另一种方式:

要将您的网站的任何特定页面/链接转向另一个页面,只需添加此代码行:

<script>
    if(window.location.href == 'old_url')
    {
        window.location.href="new_url";
    }

    // Another URL redirect
    if(window.location.href == 'old_url2')
    {
        window.location.href="new_url2";
    }
</script>

作为现实生活的例子,

<script>
    if(window.location.href == 'https://old-site.com')
    {
        window.location.href="https://new-site.com";
    }

    // Another URL redirect
    if(window.location.href == 'https://old-site.com/simple-post.html')
    {
        window.location.href="https://new-site.com/simple-post.html";
    }
</script>

使用这个简单的代码,您可以重新引导完整的网站或任何单页。

location.assign(): 指定路径通过路径到它.. 指定将给你一个历史,即使路径被分配后. 使用方法:值应该转移到它. 例如: location.assign(“http://google.com”) location.href 可以定义给一个路径到它... 它将重定向到一个特定的路径一旦它设置,它将保持历史... 使用方法:值应该分配到它


assign() 和 href 是相似的,两者都可以持有历史。 assign 将通过通过值工作, href 通过分配工作。

您可以使用 JavaScript 本身实现,而不使用 jQuery 通过分配,

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

你可以得到类似的事情使用jQuery如下。

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

你可以很容易地理解两者之间的差异。

这里是位置对象,

此分類上一篇

如果您想在同一应用程序中重新引导到路线

window.location.pathname = '/examplepath'

这将是走路的方式。

单页申请,在同一申请路线内

window.location.pathname = '/stack';

JavaScript 的内容:

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

jQuery:

$(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']);
}

在我的工作经验中,JavaScript更好地重定向。

它取决于您想要如何更改位置. 如果您想在用户历史中登录您的网站,请使用 window.location.href='ur website';. 否则,如果您不登录历史,请使用 window.location.replace(“您的网站”);。

使用 location.replace() 将重定向您,但没有保存前一页的历史。

但是,如果你想保持你的历史,你必须使用 location.href=//path。

例子:

// Form with steps
document.getElementById('#next').onclick = function() {
   window.location.href='/step2' // Iteration of steps;
}

// Go to next step
document.getElementById('#back').onclick = function() {
   window.history.back();
}

// Finish
document.getElementById('#finish').onclick = function() {
   window.location.href = '/success';
}

// On success page
window.onload = function() {
    setTimeout(function() {
       window.location.replace('/home'); // I can't go back to success page by pressing the back button
    },3000);
}

您可以写下按钮点击事件的 URL.Action 在脚本部分如下。

function onclick() {
    location.href = '@Url.Action("Index", "Home")';
}

如果您更喜欢使用纯粹的JavaScript,我意识到使用document.location.href = “https://example.com” 或 window.location.href = “https://example.com” 会导致 Firefox 中的兼容性问题。

location.href = "https://example.com";
location.replace("http://example.com");

在我看来,这个问题已经解决了,祝你好运!

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.