使用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 会出现的位置。 您要做的是将点击处理器添加到与不同页面相对应的每个锁定标记中。 此点击处理器会引用一些 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 重置

j/ 查询没有必要,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。 相反,它更喜欢纯 JavaScript 解决方案。

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

标准“ vanilla” JavaScript 重定向页面的方法

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

或更简单: (自window全球)

location.href = 'newPage.html';

如果你在这里,因为你损失HTTP_REFERERER 重定向时, 继续读取 :

(否则忽略此最后部分)


下一节针对的是那些使用HTTP_REFERER作为许多安全措施之一的安全措施(尽管这不是一个伟大的保护措施)。因特网探索者 8使用任何形式的 JavaScript 页面重定向( 位置.href 等) 时, 这些变量会丢失 。

我们将在下面实施一个替代方案:IE8 更低这样我们就不会失去 HTTP_REFERER。 否则,你几乎总是可以简单地使用 HTTP_REFERERER。window.location.href.

测试HTTP_REFERER(URL 粘贴、 会话等)能够帮助判断请求是否合法 。(注:正如Droop在评论中指出的,也有一些办法可以对这些裁判员进行变通/补充)


简单的交叉浏览器测试解决方案( 返回窗口. place.href 用于 Internet Explorer 9+ 和所有其他浏览器)

用法 :redirect('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

如果你有三种不同的环境,如发展、中转和生产,将会很有帮助。

您可以将这些单词放入铬控制台, 来探索此窗口或窗口。 位置对象 。 @ info: whatsthis火虫控制台

在单击功能上,只需添加:

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 刷新元标记的标注部分, 也是一个很好的解决方案 。 我建议您使用此JavaScript 调整方向工具以创建再定向。它也拥有互联网探索者支持以通过 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.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 区域中间,则使用 < shit> 标记。

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

在 JavaScript 和 jQuery 中, 我们可以使用以下代码将一页改到另一页 :

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

jj 查询不需要。 您可以做到这一点 :

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>

所以说,你只是把这个片段redirect/index.html在您网站上的文档中,您可以这样使用它 。

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

如果你去那个链接 它会自动引导你到堆叠流. 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 URL URL URL 網址)您也可以使用您的 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>

enter image description here

  1. 协议 -- 指定协议名称用于访问互联网上的资源。 (HTTP(没有 SSL)或HTPS(没有 SSL))

  2. 主机名 -- -- 主机名指定拥有资源的主机名。例如,www.stackoverflow.com。一个服务器使用主机名提供服务。

  3. 端口 -- -- 用于识别因特网或其他网络电文到达服务器时要发送到的具体过程的端口号。

  4. 路径 - 路径为网络客户端想要访问的主机内的具体资源提供信息。 例如, stackoverflow. com/ index.html 。

  5. 查询 -- -- 查询字符串跟随路径组件,并提供一个资源可用于某种目的的信息字符串(例如,作为搜索的参数或作为要处理的数据)。

  6. hash - URL 的锁定部分, 包括 hash 符号 (#) 。

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

  1. 散数- 发送或返回 URL 的锁定部分。
  2. 主机主机- 发送或返回 URL 主机名和端口。
  3. 主机名- 发送或返回 URL 主机名。
  4. 小时( href)- 设置或返回整个 URL 。
  5. 路径名- set 或返回 URL 的路径名。
  6. 端口端口- set 或返回服务器用于 URL 的端口号。
  7. 议定书议定书- 发送或返回 URL 的协议。
  8. 搜索搜索- set 或返回 URL 的查询部分

如果您现在想要更改页面或将用户重定向到其他页面, 您可以使用该页面href像这样的位置对象的属性

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

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

位置对象还有这三种方法

  1. (指定)- 装入新文档。
  2. 重新装入()- 重新装入当前文档。
  3. (替换)-- -- 将当前文档替换为新文档

您可以使用指派 () 并替换将重定向到其它页面的方法

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

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

转让 () 和替换 () 如何不同- 替换() 方法与指定() 方法() 之间的差别是,替换() 从文档历史中删除当前文档的 URL, 意思是无法使用“ 回溯” 按钮返回原始文档。所以,如果您想要装入新文档,则使用指定() 方法, 并想要给此选项返回原始文档 。

您可以使用jj 查询像这样

$(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
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>

# HTML 页面使用 jQuery/JavaScript 方法重定向

尝试此示例代码 :

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

如果您想要给完整 URL 提供完整 URLwindow.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');

最后一个是最好的, 用于传统的重定向, 因为它不会保存您在搜索历史中被重定向之前的页面。 但是, 如果您只想用 JavaScript 打开一个标签, 您可以使用上述任何选项 。1

爱迪特:window前缀是可选的 。

在我开始之前, 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.hrefwindow.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>

如果您将两个页面示例合并在一起, 你会有一个新生的路线改变循环, 这将保证您的用户将永远不再想要使用您的网站 。

您可以在 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>

您也可以这样操作, 点击按钮时使用位置. 指派 :

<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

jjQuery 允许您从网页中轻松选择元素。 您可以在页面中找到任何您想要的内容, 然后使用 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);
});

想象一下有人写了一个脚本/ 插件, 包含 10000 行代码 。 如果有 jQuery , 您可以用一两行代码连接到这个代码 。

我只是不得不更新这种荒谬的 另一种新的jQuery方法:

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

标注描述 :

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

使用 j 查询 :

$(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/")

jj 用于重定向页面或 URL 的查询代码

第一路

这是用于重定向页面的 jQuery 代码 。 既然, 我已经把这个代码放在$( document). ready () 函数上, 一旦页面加载, 它将会立即执行 。

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

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

第二路

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

您也可以使用这样的代码(两者在内部相同):

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

如果您对窗口. 位置和window.location.href,然后你可以看到后一个设置href明确表明财产,而前者默示地这样做。window.location返回一个对象,该对象默认设置它.href财产。

第三路

使用 JavaScript (JavaScript)replace()方法方法的计算方法的计算方法window.location对象。您可以将新的 URL 传送到replace()方法, 它将模拟 HTTP 重定向。 顺便说一下, 记住window.location.replace()方法不会在会话历史中放置原始页面, 这可能影响到后按钮的行为。 有时, 它会是你想要的, 所以要小心使用它 。

// Doesn't put originating page in history
window.location.replace("http://stackoverflow.com");

第四路

缩略location.assign()方法在浏览器窗口中装入新文档。

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

和(或)assign()replace()方法为location.replace()从文档历史中删除当前 URL 的方法,因此无法返回原始文档。您不能使用浏览器。后退按钮。如果您想要避免这种情况,应该使用location.assign()方法,因为它在浏览器中装入一个新的文档。

第五路

类似attr()方法 (在 jQuery 1. 6 引入后)

var url = "http://stackoverflow.com";
$(location).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>Redirecting to another page</h2>
        </div>

        <script src="scripts/jquery-1.6.2.min.js"></script>
        <script>
            // JavaScript code to redirect a URL
            window.location.replace("http://stackoverflow.com");
            // window.location.replace('http://code.shouttoday.com');

            // Another way to redirect page using JavaScript

            // window.location.assign('http://code.shouttoday.com');
            // window.location.href = 'http://code.shouttoday.com';
            // document.location.href = '/relativePath';

            //jQuery code to redirect a page or URL
            $(document).ready(function(){
                //var url = "http://code.shouttoday.com";
                //$(location).attr('href',url);
                // $(window).attr('location',url)
                //$(location).prop('href', url)
            });
        </script>
    </body>
</html>

ECMAScript 6 + jQuery,85 字节

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

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

这“为问题提供了答案”, 意思是它要求找到“使用jQuery”的解决方案,

Ferrybig显然需要解释笑话(还是开玩笑,

其他的答案是使用 jQuery 的attr()会 议 日 程 和 议 程locationwindow不必要的物体。

这个答案也滥用了它, 但以一种更荒谬的方式。 它不是用它来设定位置, 而是用它来设置位置,attr()获取设置位置的函数。

函数被命名jQueryCode即使它没有任何jQuery, 并且号召一个函数somethingCode特别是当事情甚至不是语言的时候。

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

缩略replace()方法不会保存会话历史中的源页, 因此用户无法使用后端按钮返回, 然后再被重定向 。 注意: 在此情况下, 浏览器后端按钮将被禁用 。

但是,如果您想要与点击链接相同的效果,您应该选择:

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

在此情况下, 浏览器后端按钮将激活 。

这很容易执行。 您可以使用 :

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

这将记住上一页的历史。 这样可以点击浏览器的后键返回历史 。

或者:

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

此方法不记得上一页的历史。 在此情况下, 后端按钮会被禁用 。

您可以使用以下方法重定向页面:

  1. 通过在头部使用元标记 -<meta http-equiv="refresh" content="0;url=http://your-page-url.com" />注意:content="0;... 用于在您需要多少秒后重定向页面

  2. 使用 JavaScript :window.location.href = "http://your-page-url.com";

  3. 使用 jQuery 查询 :$(location).attr('href', 'http://yourPage.com/');

使用 :

function redirect(a) {
    location = a
}

并称之为:redirect([url]);

没必要href之后location,因为它是隐含的。

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>

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

使用 j 查询 :

方法1:美元(地点)

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

方法2:可再使用功能

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

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

基本上jj 查询只是一个JavaScript 贾斯克里普特和做一些诸如转向在此情况下, 您可以使用纯 JavaScript, 这样您就有三个选择, 使用 vanilla JavaScript :

(1) 使用地点替换,这将取代页面的当前历史,这意味着无法使用后退按钮返回原始页面。

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

2) 使用地点指派,这将保留您的历史,如果使用后按钮,您可以回到原始页面:

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

3) 我建议使用其中一种方法, 但这可能是使用纯JavaScript的第三个选择:

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

您也可以在 jQuery 中写入一个函数来处理它, 但由于它只是一行纯 JavaScript 函数, 您也可以在 jQuery 中写入一个函数来处理它, 但由于它只是一行纯 JavaScript 函数, 您也可以使用所有上面没有窗口的函数, 如果您已经进入窗口范围, 例如window.location.replace("http://stackoverflow.com");可以是location.replace("http://stackoverflow.com");

我还在下面的图像上展示了所有这些照片:

location.replace location.assign

我只是补充另一种方式:

要将网站的任何特定页面/链接重定向到另一页,请添加这一行代码:

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

通过使用这个简单的代码, 您可以重定向整个网站或任何单页 。

  1. 指定位置( ) :

    将一条路径通过一条路径进入它 来指定一条路径...指派即使在指定路径之后, 也会给你们一个历史 。

    用法:数值应传递到它中 。

    例如: location.assign("http://google.com")

    Enter image description here

  2. 位置.href

    定义可以给它一个路径... 并且一旦它建立,它会重新定位到一个指定路径, 它会保存历史...

    用法:值应该被指定到它中 。

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

  3. 位置. replace () :

    如果您不想保留历史, 它会帮助您替换一条路径。 一旦您替换了一条路径, 它不会给您一个历史 。

    用法:数值应传递到它中。

    例如: location.replace("http://google.com")

    Enter image description here


assign()href两者相似,可以保留历史。assign通过分配,通过传递一个价值和粗略的作品来工作。

您可以使用 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");

你可以很容易地理解 两者之间的区别...

这里是定位对象,

Location API from chrome

如果您想要直接重定向到同一应用程序内的路线, 只需

window.location.pathname = '/examplepath'

这将是前进的道路。

单页应用在同一申请路线内

window.location.pathname = '/stack';

JavaScript :

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

jj 查询 :

$(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("your website");.

使用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. 动作 。

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.