How to execute some jQuery or JavaScript code after a redirect has completed

后端 未结 4 1626
心在旅途
心在旅途 2021-01-20 00:21

How can I execute jQuery/JS code as soon as a jQuery/JS redirect like...

$(location).attr(\'href\',\'/target_path\');

...or...



        
4条回答
  •  不知归路
    2021-01-20 01:21

    var x = "It's some text";
    var loc = '/target_path';
    
    loc += '?message=' + encodeURI(x);
    

    The JS file on the new page can then look at the query string to see if a message is there, and do the required action if it's detected.

    You can use window.location.search on the new page to see what's there, although I'd recommend hunting for a deparam function in a library to turn the query string into a more usable object.

提交回复
热议问题