1

I tried to call javascript function in my xhtml page by using RequestContext but I could not succeed it. Is there a way to call javascript function from view bean at prime faces ?

If the questions is not clear then I can provide code.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Tonyukuk
  • 5,745
  • 7
  • 35
  • 63
  • Do you mean you want the server to execute JavaScript? Or do you want the browser to execute JavaScript that is part of the server page? – Steve H. Jun 19 '17 at 20:16
  • Hello steve i actually meant the server to execute javascript. – Tonyukuk Jun 20 '17 at 05:21
  • 1
    Are you calling like this: `RequestContext.getCurrentInstance().execute("yourScriptFunctionName();");` Also, which version of JSF / PrimeFaces are you using? – Parkash Kumar Jun 20 '17 at 06:06
  • hello parskash. Exactly. I am trying to call RequestContext.getCurrentInstance().execute("javascript function"); I am usingg primefaces 6.0 and jsf 2.2.4 – Tonyukuk Jun 20 '17 at 06:09
  • if still not clear i can provide sample code – Tonyukuk Jun 20 '17 at 06:22
  • did you see this post [Calling a JS function from ManagedBean ?!?](https://stackoverflow.com/questions/5675017/calling-a-javascript-function-from-managed-bean) – Yagami Light Jun 20 '17 at 07:35
  • you should **always** provide code if things are not working. See [ask]. And the code should be in [mcve] format – Kukeltje Jun 20 '17 at 14:52
  • HYmm.... cannot vote 'mark as duplicate' of the question referred to by @YagamiLight... it **is** a duplicte – Kukeltje Jun 20 '17 at 14:54
  • It is the same thread [https://stackoverflow.com/questions/5675017/calling-a-javascript-function-from-managed-bean](https://stackoverflow.com/questions/5675017/calling-a-javascript-function-from-managed-bean) – Jonathan Marin Feb 12 '21 at 22:39

1 Answers1

2

Like @Parkash Kumar said, you can call it from the bean, like this:

RequestContext.getCurrentInstance().execute("alert('script called.')");

or directly from the xhtml:

<p:commandButton value="Save" actionListener="#{requestContextView.save}" oncomplete="handleComplete(xhr, status, args)" />
danny
  • 358
  • 2
  • 14
  • RequestContext.getCurrentInstance().execute("alert('script2 called.')"); does not work at my bean Danny. Dont know the reason work. But I am not able to see any alert – Tonyukuk Jun 20 '17 at 07:23
  • Check that there's no exception thrown on the server side. Also check your browser console for output. You could also use a `p:growl` to collect error messages – danny Jun 20 '17 at 07:52
  • 2
    yea . there was a problem at clientside. that's why even it does not recognize RequestContext.getCurrentInstance() method . Thanks – Tonyukuk Jun 20 '17 at 08:26
  • There are numerous duplicates of this question and answer. Please next time search for them and mark a question as such. This prevents 'fragmentation' of knowledge – Kukeltje Jun 20 '17 at 14:52
  • @Kukeltje not sure how this is even relevant. – Makky Mar 27 '19 at 16:57
  • @Makky: Then read the last sentence of my comment again an try to find in a search engine why Stackoverflow has the option of flagging and closing as a duplicate. – Kukeltje Mar 27 '19 at 18:00
  • @Kukeltje I agree at some extent but I do see this problem on SO. A lot of people who have some knowledge feel everyone would have same knowledge. As you can see soeone actually answerd and been accepted and I also found this useful. – Makky Mar 27 '19 at 18:13