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

I have snippent code which is executed by clicking on the link 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 the above code window.location.href=base_url+"main"; redirects the page to the corresponding page, but this stops the execution of the code written next to it. Now I want this code to run the way it was written: first, the code should take me to the corresponding "main" page, and then write the text after it should execute and provide me with the required output.

Can someone help me achieve this?

+6
source share
5 answers

window.location.href = base_url + "main"; <- when loading this page, call the code defined in ///next

you will need to add some parameters:

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

Another way is to load the page using ajax in a div in html.

Take a look at $.ajax() on jQuery.

+9
source

try to write

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

shortly before the end of the if condition or use

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

As already noted, you cannot execute the code after switching to another page

What you can do is create a redirect function that will pass your function to a cookie and redirect and then check it on the next page. (with another call to this redirector on the next page)

But you need to know the number of problems

1 package. It's up to you how you pack the cookie.

2) Registration. If you send an unpacked or unencrypted cookie, a β€œbad user” may transmit malicious code inside this cookie.

3) You must have VERY, VERY good reasons for this. This method is too complicated, difficult to program, difficult to maintain

Much better if you are running additional server controls, save them somewhere and reload another request on the next page.

+2
source

You will need to wrap the rest of the JS code in the window.onbeforeunload callback window.

See here: Capture Page Exit Event

+1
source

You need to use the onBlur event to continue executing js code before exiting 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(); } } 
0
source

Source: https://habr.com/ru/post/1382238/


All Articles