execute a function after redirecting - javascript

前端 未结 3 1398
误落风尘
误落风尘 2020-11-30 10:25

Okay, i have a simple button on my page (MyPage) which fades out the current div (fade 1) and fade in another one (fade 2). I have now realised that there might be chances t

相关标签:
3条回答
  • 2020-11-30 11:03

    you can do it with sessionStorage()

    $('#fav').click(function(){
      sessionStorage.setItem("reloading", "true");
      window.location = 'production/produc_order.php';
    });
    var reloading = sessionStorage.getItem("reloading");
    if(reloading == true) {
      sessionStorage.removeItem("reloading");
      $('#view_production').fadeOut('slow');
      $('#create_order').fadeIn('slow');
    }
    
    0 讨论(0)
  • 2020-11-30 11:08

    Changing the window.location will kill all scripts currently running in the browser.

    Your only other solution is getting a page via AJAX and run a callback function to execute when the content is loaded. Here is something to get you started.

    Also, jQuery as a nice .ajax() method to easily perform AJAX requests and associate callbacks to successful and failed requests.

    0 讨论(0)
  • 2020-11-30 11:23

    If you don't want to or can't re-code your page to support AJAX, the other old-school option is to pass a parameter in the URL as a hint to the refreshed page. (You can hide it by making the redirect a POST if you feel it's really necessary, or use a cookie technique. The point is that the refreshed page needs a token of some form from the prior page.)

    eg:

    $('#fav').click(function(){
        window.location = 'production/produc_order.php?create=1';
    })
    

    and put the fade code inside the $(document).ready() function, with a check for the create parameter, cookie or whatever.

    I'll agree with @remibreton though, using AJAX is the more hip, modern method.

    0 讨论(0)
提交回复
热议问题