gpt4 book ai didi

javascript - jsf 命令按钮中的 onclick 在 chrome 中不起作用

转载 作者:行者123 更新时间:2023-11-30 18:09:39 25 4
gpt4 key购买 nike

我在 <h:commandButton> 中调用 JavaScript通过 onclick事件。

 <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

当我浏览论坛时,我看到这是为了防止存在 onclick 时的跨站点脚本。与 POST正如这个问题中所解释的

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.

我该如何解决这个问题?我正在使用 JSF 2.0 和 Apache myfaces 实现。

最佳答案

怎么样

<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;"/>

关于javascript - jsf 命令按钮中的 onclick 在 chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14907013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com