重定向前调用JS函数 [英] Call JS function before redirect

查看:160
本文介绍了重定向前调用JS函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如下: 客户端上的用户输入一些数据,然后按下启动ajax请求的命令按钮.根据输入数据,服务器端的JSF Bean重定向到不同的页面.但是除了HTML5的本地存储功能外,会话存储还应在客户端进行更新,这也取决于Bean的结果.对于存储,必须从用户输入一些数据,因此存储的Java脚本必须在原始页面内.

The situation is as follows: A user on the client side enters some data and presses a command button that initiates an ajax request. Depending on the input data the JSF Bean on the server side redirects to different pages. But in addition the HTML5 features localstorage respectively sessionstorage should be updated on client side, also depending on the results from the bean. For the storage some data entered from the user is necessary, so that the java script for the storage has to be within the original page.

现在,问题似乎出在响应之后,始终总是先执行重定向,然后再执行js函数,因此无法从curse客户端的用户访问输入数据.

Now, the problem seems to be that after the response the redirect is always done first, before executing the js function and so it is not possible to access the input data from the user on the client side of curse.

在客户端使用回调参数(重定向发生较早)或从服务器端执行RequestContext.execute(例如,在内部)上按下"oncomplete"之类的命令按钮后,我尝试了调用js函数的操作(但这在内部也使用oncomplete事件)我认为). 我可以想象的一种可能的解决方案是在客户端使用window.onunload和一个隐藏的输入公式来获取bean的结果.但是没有更好的解决方案吗?

I tried things for calling the js function after pressing the command button like "oncomplete" on the client side using callback parameters (redirect takes place earlier) or RequestContext.execute from the server side (but this internally also uses the oncomplete event i think). One possible solution I could imagine is to use window.onunload on client side and a hidden input formular to get the result of the bean. But isn't there a better solution?

推荐答案

您需要通过JS而不是JSF进行重定向.从操作方法返回null,使其返回同一页面,然后执行以下脚本以在JS中执行重定向.

You need to redirect by JS instead of by JSF. Return null from action method so that it returns to the same page and then execute the following script to perform a redirect in JS.

window.location = newURL;

这篇关于重定向前调用JS函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆