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

5

7

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.

Can someone guide me to achieve this?

Unbuckle answered 21/11, 2011 at 9:23 Comment(3)
this wont work like that, when you redirect you get another page load, that means you loose everything you hold in your javascript memory. Add a parameter to the redirect, by which you know when you redirected and hanle it on page loadMulloy
since you tagged jquery I wonder which code is jquery hereMulloy
please change this code document.getElementById('content').innerHTML=response; to this $('#content').html(response);Mulloy
M
12

window.location.href = base_url + "main"; <- when you load this page, call your code defined at ///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.

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

Mulloy answered 21/11, 2011 at 9:33 Comment(2)
U seem comparatively more sensible here... I am trying yout ideaUnbuckle
thanks a lot for ... load the page with ajax into a div in the html.Unquestioning
C
3

please try to write

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

just before the end of if condition or use

setTimeout('window.location.href=base_url+"main"', 2000);
Chiffon answered 21/11, 2011 at 9:28 Comment(1)
he is trying to redirect the page and then process further. If he had the problem to redirect at a later time, he could have moved the code downMulloy
A
2

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)

But you should be aware of number of issues

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

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

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

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

Angulation answered 21/11, 2011 at 9:39 Comment(0)
M
1

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

See here: Intercept page exit event

Multiform answered 21/11, 2011 at 9:35 Comment(0)
D
0

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

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(); 
        }
}
Deutero answered 24/9, 2019 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.