1

I am invoking JavaScript in <h:commandButton> by onclick event.

 <h:commandButton type="submit" 
          value="Next" action="#{bean.save}"    
          onclick="javascript:getHtml();"/>

 function getHtml(){
      document.getElementById('source').value="HTML source of the page";

  }

output HTML for commandButton in IE/Firefox

<input id="Form:submit" name="Form:submit" 
           type="submit" value="Next"
           onclick="var cf = function(){getHtml();};var oamSF = function(){return 
myfaces.oam.submitForm('Form','Form:submit',null,[['sample','sample']]);};return (cf.apply(this, [])==false)? false : oamSF.apply(this, []);">

But in chrome I see the below JavaScript error with the below HTML

Refused to execute a JavaScript script. Source code of script found within request.

<input id="Form:submit" name="Form:submit" 
       type="submit" value="Next" 
       onclick="" >  // onclick is empty

when i went through forums I see this is to prevent against Cross site scripting when there is onclick with POST as explained in this question

Refused to execute a JavaScript script. Source code of script found within request

This happens when some JavaScript code is sent to the server via an HTTP POST request, and the same code comes back via the HTTP response. If Chrome detects this situation, the script is refused to run, and you get the error message Refused to execute a JavaScript script. Source code of script found within request.

How can i Fix this ? I am using JSF 2.0 with Apache myfaces implementation.

Community
  • 1
  • 1
SRy
  • 2,901
  • 8
  • 36
  • 57
  • Did you try just to remove `javascrip:`? – partlov Feb 16 '13 at 07:43
  • Yes.I tried by removing `javascript:` but no use. Not able to invoke the javascript. – SRy Feb 17 '13 at 03:11
  • @partlov......Do I get any issue if i call same javascript by using `` instead of `onclick` in `commandbutton` – SRy Feb 17 '13 at 04:14
  • Maybe you need to show some more of your jsf page here. I'm struggling to understand how the javascript code would wind up in the request sent to the server. Have you observed your browser's console to check the generated markup for irregularities? – kolossus Feb 17 '13 at 21:18
  • @kolossus...Hi I have updated my OP.If you see in the output HTML of `commandButton`in chrome `onclick` is empty – SRy Feb 18 '13 at 20:07
  • If I remember correctly I was having similar issues and adding defer attribute to the script tag helped – David Tolioupov Feb 18 '13 at 20:09
  • @DavidTolioupov..I added the defer.Still it's not working. Is this a bug in Chrome? – SRy Feb 18 '13 at 20:52

1 Answers1

2

How about

<h:commandButton type="submit" 
      value="Next" action="#{bean.save}"    
      onclick="document.getElementById('source').value='HTML source of the page'; 
      return false;"/>

If this works for you than you didn't include or placed your js file properly in your page / project ...


A better solution would be to properly include your js file

like this

<h:outputScript name="js/myFile.js" target="head"/>

Place your myFile.js inside WebContent\resources\js

Than use it like this

<h:commandButton type="submit" 
      value="Next" action="#{bean.save}"    
      onclick="getHtml(); return false;"/>
Daniel
  • 36,833
  • 10
  • 119
  • 200
  • @Daniel...Thank you very much. This is working perfectly fine. What is the problem when i included script with ` – SRy Feb 18 '13 at 21:25
  • You are welcome , You better try with ` //Some code goes here... ` But an even better solution would be the one I posted in the updated answer... – Daniel Feb 18 '13 at 21:41
  • @Daniel..Unfortunately after rigorous testing this approach is also failing. It's very inconsistent. For some time it's working and for some time it's not. What Instead of `onclick` in the `commandButton`,if I use `onsubmit` in the ``. Will this create any issue? – SRy Feb 19 '13 at 21:52
  • sounds like a bad idea... instead try to find out why its so inconsistent , and also maybe there is another way to achieve your goal (which I'm not so sure what it is...) maybe try to ask a new question : How to achieve certain goal... – Daniel Feb 20 '13 at 07:29