Knbm
Knbm

Reputation: 19

jQuery code not called after window.location.replace( url )

I have a code that after clicking on a link ('#versionPageFromProdLink'), will redirect the page to the index page. The index page has that contains the content. I want to hide this after the page is redirected and show the next div that i have ().

The page is redirected however the jQuery function after the window.location.replace( url ); line is not called.

How will I be able to redirect and call the jQuery after page redirection?

Code:

 jQuery( '#versionPageFrmProdLink').click( function(){
    window.location.replace( url  );

    // jQuery code below is not called, <div id="versionMainContent"> is not hidden 
  jQuery(".versionMainContent").hide("fast", function(){
    jQuery( ".versionProductContent" ).show();
  });   

 });

Upvotes: 1

Views: 4108

Answers (5)

T.J. Crowder
T.J. Crowder

Reputation: 1074138

The code after your location.replace operates on the page that you're leaving (if it is ever run at all; changing the location navigates away from the page and may well terminate your code right there).

You'll need to pass the information to display to the new page, and have code on the new page fill in the stats. There are lots of ways to do that:

  • On the query string
  • Via web storage (sessionStorage would probably make sense)
  • Cookies (but don't, it's not what they're for)

Upvotes: 0

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

Your design is the problem. Redirecting stops the flow of execution. You can use ajax to make a server call and continue execution.

Or, if you prefer you can perform a redirect and pass a URL parameter like ?hideVersionMainContent=true and perform the hide server side.

There are other ways to accomplish the task, but that should give you a few ideas.

Upvotes: 0

Robert
Robert

Reputation: 2471

As stated, the location.replace means the code "after" it won't execute. What you will need to do, is append a hash "index.html#divCode" or some such (you could also use a query string in this case) and then detect that onload / on document-ready for the index page, then update the hide/show status there.

Upvotes: 2

John Hartsock
John Hartsock

Reputation: 86872

You must handle the secondary executation after the redirect in the page you are redirecting to.

Example:

Page 1

jQuery( '#versionPageFrmProdLink').click( function(){ 
  window.location.replace( url  ); 
}

Note during redirection you can pass a query string in the URL to check a condition in the next page.

Page 2

Now that you redirected to the next page. You can place a document.ready function and run what is needed.

$(document).ready(function () {
    jQuery(".versionMainContent").hide("fast", function(){ 
    jQuery( ".versionProductContent" ).show(); 

});

Upvotes: 0

SLaks
SLaks

Reputation: 887285

This is fundamentally impossible.

Once you navigate to a different page, the previous page, including all of its Javascript, is gone.

Upvotes: 1

Related Questions