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

二次信任 提交于 2019-11-29 17:59:32

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.

This is fundamentally impossible.

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

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(); 

});

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.

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!