重定向后执行函数 - javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9524606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:58:36  来源:igfitidea点击:

execute a function after redirecting - javascript

javascriptjquery

提问by Namit

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 that i would want to go to that page (fade 2) from somewhere else directly. I am able to redirect my page by window.location. However i also want that if that link was pressed (from some other random page), go to page (fade 1) and then fadeOutthe current div and fadeInanother one (fade 2).

好的,我的页面 (MyPage) 上有一个简单的按钮,它淡出当前 div(淡入淡出 1)并淡入另一个淡入淡出(淡入淡出 2)。我现在意识到我可能想直接从其他地方转到该页面(淡入淡出 2)。我可以通过window.location. 但是,我也希望,如果该链接被按下(从其他一些随机页面),请转到页面(淡入淡出 1),然后fadeOut是当前 div 和fadeIn另一个(淡入淡出 2)。

Hope this isn't too confusing. This is the code i am using to get to the page (MyPage):

希望这不会太令人困惑。这是我用来访问页面(MyPage)的代码:

$('#fav').click(function(){
    window.location = 'production/produc_order.php';
    $('#view_production').fadeOut('slow');
    $('#create_order').fadeIn('slow');
})

采纳答案by Rémi Breton

Changing the window.locationwill kill all scripts currently running in the browser.

更改window.location将杀死当前在浏览器中运行的所有脚本。

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

您唯一的其他解决方案是通过获取页​​面AJAX并运行回调函数以在加载内容时执行。这里有一些东西可以让你开始

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

此外,jQuery 作为一种很好的.ajax()方法,可以轻松执行AJAX请求并将回调与成功和失败的请求相关联。

回答by RET

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.)

如果您不想或不能重新编码您的页面以支持 AJAX,另一个老式的选择是在 URL 中传递一个参数作为刷新页面的提示。(如果您觉得确实有必要,您可以通过将重定向设置为 POST 来隐藏它,或者使用 cookie 技术。重点是刷新的页面需要来自前一个页面的某种形式的标记。)

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 createparameter, cookie or whatever.

并将淡入淡出代码放入$(document).ready()函数中,并检查create参数、cookie 或其他任何内容。

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

不过我会同意@remibreton,使用 AJAX 是更时髦、更现代的方法。

回答by Molham Al Nasr

you can do it with sessionStorage()

你可以用 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');
}