如何在 JavaScript 中更改 window.location.href,然后执行更多 JS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8209626/
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-24 05:08:13  来源:igfitidea点击:

How to change window.location.href in JavaScript and then execute more JS?

javascriptjquerywindow.location

提问by OM The Eternity

I have code snippent which is executed on click of a link which is as below

我有代码片段,点击下面的链接即可执行

cj_redirecturl="/HomeWork/main/load_course";
    utility.xhttprw.data(cj_redirecturl,{data:courseid},function(response){
        if(response){
            window.location.href=base_url+"main";

///next
utility.xhttprw.data(redirecturl,{data:""},function(response){
        if(response){

            id=ent;
            document.getElementById('content').innerHTML=response;
             // Assignemnt module
             //Call function from javascript/assignment.js file to display particular assignment  (ent:means assignment_id) //  
                if(con==5)
                {
                    get_assignment_id(ent);
                }
                if(con==9)
                {
                    get_content_details(ent);
                }


        }   //end of response       
    },false,false,false,'POST','html',true); 
        }
    },false,false,false,'POST','html'); 

in above code window.location.href=base_url+"main";redirects the page to its respective page but this stops the execution of the code written just next to it. Now I want this code to be executed as it is been written i.e. firstly the code should take me to the respective "main" page and then code writte after that must execute and give me a required output.

上面的代码window.location.href=base_url+"main";将页面重定向到其各自的页面,但这会停止执行紧挨着它的代码。现在我希望这段代码在编写时执行,即首先代码应该将我带到相应的“主”页面,然后编写代码之后必须执行并给我所需的输出。

Can someone guide me to achieve this?

有人可以指导我实现这一目标吗?

回答by Rumplin

window.location.href = base_url + "main";<- when you load this page, call your code defined at ///next

window.location.href = base_url + "main";<- 当您加载此页面时,调用您定义的代码 ///next

you will have to add some parameters:

您将不得不添加一些参数:

window.location.href=base_url+"main?parameter=true";

The other way would be to load the page with ajax into a div in the html.

另一种方法是将带有ajax的页面加载到html中的div中。

Have a look at $.ajax()from jQuery.

$.ajax()从 jQuery看一看。

回答by Shailesh Jangir

please try to write

请试着写

window.location.href = base_url + "main";

just before the end of if condition or use

就在 if 条件或使用结束之前

setTimeout('window.location.href=base_url+"main"', 2000);

回答by Tigra

As already been noticed, you cant execute code after you went to another page

正如已经注意到的那样,您转到另一个页面后无法执行代码

What you can do is to create redirector function, that will pass your function in cookie and ,redirect and then eval it on next page. (with another call to that redirector on next page)

您可以做的是创建重定向器函数,它将在 cookie 中传递您的函数,然后重定向,然后在下一页上对其进行评估。(在下一页再次调用该重定向器)

But you should be aware of number of issues

但是你应该知道问题的数量

1) Packing. It is upon you to decide how you pack cookie.

1) 包装。由您决定如何包装 cookie。

2) Encription. If you pass non-packed OR non-encrypted cookie the "bad user" can pass some malware code inside that cookie.

2) 刻录。如果您传递未打包或未加密的 cookie,“坏用户”可能会在该 cookie 中传递一些恶意软件代码。

3) You should have VERY, VERY good reasons to do it. This way is too complicated, hard to code, hard to maintain

3)你应该有非常非常好的理由去做这件事。这种方式太复杂,难编码,难维护

Much better if you do additional server-side controls, save it somewhere and reload on next page with one more request.

如果您执行其他服务器端控件,将其保存在某处并在下一页重新加载一个请求,那就更好了。

回答by Chuckie B

You'll need to wrap the rest of the JS code inside a window.onbeforeunload callback.

您需要将其余的 JS 代码包装在 window.onbeforeunload 回调中。

See here: Intercept page exit event

请参阅此处:拦截页面退出事件

回答by netluke

You need to use the onBlur event to continue to execute js code before exit from the page.

退出页面前需要使用onBlur事件继续执行js代码。

Example:

例子:

function downloadExcel()
{
// Your code...

        $('#download_excel_button').hide();
        $('#download_spinner').show();

        window.location.href = download_page;

        window.onblur = function(event) 
        { 
            // other event code...

            $('#download_spinner').hide();
            $('#download_excel_button').show(); 
        }
}