jQuery 如何在调用 window.location.href 后执行脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18048338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:25:42  来源:igfitidea点击:

How can I execute a script after calling window.location.href?

javascriptjquery

提问by Stephanie Caldwell

I have a script that redirects the user to another page. I want to load some content into a div on the new page after the new page has fully loaded. How can I do this. The following doesn't work.

我有一个将用户重定向到另一个页面的脚本。我想在新页面完全加载后将一些内容加载到新页面上的 div 中。我怎样才能做到这一点。以下不起作用。

function goToPage() {
    window.location.href = 'http://www.mypage.com/info;
    $('.my_class').load('my/url/path/with/content/to/load');
}

The newly loaded page (http://www.mypage.com/info) contains the following div:

新加载的页面 ( http://www.mypage.com/info) 包含以下 div:

<div class="my_class"></div>

What am I doing wrong?

我究竟做错了什么?

回答by Tombatron

Redirect to the new page, but append a hash signal to the URL.

重定向到新页面,但将哈希信号附加到 URL。

function goToPage() {
    window.location.href = 'http://www.mypage.com/info#load-stuff;
}

Then on load of the target page, evaluate the URL checking for that hash signal.

然后在加载目标页面时,评估该哈希信号的 URL 检查。

function pageLoad() {
    if (window.location.hash === "#load-stuff") {
        $('.my_class').load('my/url/path/with/content/to/load');
    }
}

If your application is using jQuery it'd look something like:

如果您的应用程序使用 jQuery,它看起来像:

$(function () {
    if (window.location.hash === "#load-stuff") {
        $('.my_class').load('my/url/path/with/content/to/load');
    }
});

That's the rough idea at least.

至少这是一个粗略的想法。

回答by Hauke P.

As pointed out in the other answers, you won't be able to perform any script instructions from your original site. Instead of using PHP to create the content statically, you could also use HTML fragments as arguments, e.g. like this:

正如其他答案中所指出的,您将无法从原始站点执行任何脚本指令。除了使用 PHP 静态创建内容之外,您还可以使用 HTML 片段作为参数,例如:



// in the original page:
function goToPage() {
    window.location.href = 'http://www.mypage.com/info#my/url/path/with/content/to/load';
}


// in http://www.mypage.com/info:
$( document ).ready(function () {
    if(window.location.hash)
        $('.my_class').load(window.location.hash.substring(1));
}

回答by Bill Criswell

You should just run

你应该跑

$('.my_class').load('my/url/path/with/content/to/load');

on this page: http://www.mypage.com/info.

在此页面上:http: //www.mypage.com/info

When you do window.location.href = 'http://www.mypage.com/info';you're redirecting to another page. Nothing after that line will happen. You have to instead run the code after that line on the page that's loaded.

当你这样做时,window.location.href = 'http://www.mypage.com/info';你正在重定向到另一个页面。在那条线之后什么都不会发生。您必须改为在加载的页面上的该行之后运行代码。

回答by Radmation

An easy way to pass data to your page you are redirecting to would be to set some url parameters.

将数据传递到您要重定向到的页面的一种简单方法是设置一些 url 参数。

For example:

例如:

window.location.href - "http://youpage.com/?key=value"

When that page loads you could have a:

当该页面加载时,您可能有:

$(document).ready(function(){

    var my_param = getUrlParameter('key');
    if(my_param == "value"){
//do your stuff here
}

});


var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

回答by theMightyT

You can do this a few different ways. Try leveraging the localstorage API and passing info or content with a name and value pair (or a few of them) and unpack it on the receiving end.

您可以通过几种不同的方式来做到这一点。尝试利用 localstorage API 并使用名称和值对(或其中一些)传递信息或内容,然后在接收端解压。

On the page you're redirecting to, check for the localstorage key, and then load the contents of it (the aforementioned name and value pairs) into a div.

在您重定向到的页面上,检查 localstorage 键,然后将其内容(上述名称和值对)加载到 div 中。

As an alternative, you can write one script file that you can deploy to several pages; do a check on window.location.href and conditionally load script accordingly. If you're on the redirected page, you can run whatever script you like. The nice part about doing it this way is that you're still working with one JS file - no need to fragment your code (assuming, of course, that the pages you're working with are all on the same site).

作为替代方案,您可以编写一个可以部署到多个页面的脚本文件;检查 window.location.href 并相应地有条件地加载脚本。如果您在重定向页面上,则可以运行您喜欢的任何脚本。这样做的好处在于您仍然使用一个 JS 文件 - 无需分割您的代码(当然,假设您正在使用的页面都在同一个站点上)。

You don't need to do anything with php if you don't want to, or hashes... there's a few nifty tools that will do the trick if you can leverage HTML5 and its associated APIs.

如果你不想,你不需要对 php 做任何事情,或者散列......如果你可以利用 HTML5 及其相关的 API,有一些漂亮的工具可以解决这个问题。

回答by javad hemati

window.location = '#/MyPage';
setTimeout(function() {
   //MyCode To Run After PageLoad
});

回答by pattyd

Once you redirect you can no longer execute scripts on that page, as the page is unloaded.

重定向后,您将无法再在该页面上执行脚本,因为该页面已卸载。

Try this,

尝试这个,

redirect page:

重定向页面:

function goToPage() {
window.location.href = 'http://www.mypage.com/info;
}

mypage.com/info:

mypage.com/info:

js:

js:

$('.my_class').load('my/url/path/with/content/to/load');

html:

html:

<div class="my_class"></div>

I hope this helped you out, and let me know if you need further assistance!

我希望这对您有所帮助,如果您需要进一步的帮助,请告诉我!

回答by Alfie

You are redirecting the browser with window.location.hrefand I'm afraid as you are purely just changing the browser's location, you can't have any affect/input on the page you are moving to (unless you use query string parameters and then create content with something like PHP (myurl.php?newcontent=whatever) )

您正在重定向浏览器,window.location.href恐怕因为您纯粹只是在更改浏览器的位置,所以您无法对要移动到的页面产生任何影响/输入(除非您使用查询字符串参数,然后使用某些内容创建内容)像 PHP (myurl.php?newcontent=whatever) )