1

我正在<h:commandButton>onclick事件调用 JavaScript。

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

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

  }

在 IE/Firefox 中为 commandButton 输出 HTML

<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, []);">

但在 chrome 中,我看到下面的 JavaScript 错误和下面的 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

当我浏览论坛时,我看到这是为了防止出现跨站点脚本onclickPOST如本问题所述

拒绝执行 JavaScript 脚本。在请求中找到的脚本源代码

当一些 JavaScript 代码通过 HTTP POST 请求发送到服务器,并且相同的代码通过 HTTP 响应返回时,就会发生这种情况。如果 Chrome 检测到这种情况,脚本会被拒绝运行,并且您会收到错误消息拒绝执行 JavaScript 脚本。在请求中找到的脚本源代码。

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

4

1 回答 1

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;"/>
于 2013-02-18T20:59:57.040 回答