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.

4

1 に答える 1

2

どうですか

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

これがうまくいく場合は、jsファイルをページ/プロジェクトに適切に含めたり配置したりしませんでした...


より良い解決策は、jsファイルを適切に含めることです

このような

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

myFile.jsをWebContent\resources\js内に配置します

このように使用するよりも

<h:commandButton type="submit" 
      value="Next" action="#{bean.save}"    
      onclick="getHtml(); return false;"/>
于 2013-02-18T20:59:57.040 に答える