responseText contains javascript code and the code doesn't load

若如初见. 提交于 2019-12-08 09:27:09

问题


The string:

<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6..."></script>

comes from a XMLHttpRequest() request, and generated by php, and is then written to a new window with javascipt, but the script is not loaded. The window opens blank, despite the source code contains that string.

Can it be a encoding problem?

The string is the result of 'echo recaptcha_get_html($publickey)', where is the PHP function provide by the recaptcha script.


回答1:


Creating script tags by setting .innerHTML will generally not result in those scripts being executed. There are exceptions, like having the "defer" attribute set on the script tag on IE browsers, but generally it's probably not the way to go. (I'm not sure what the "async" attribute would do. I could see async="true" resulting in scripts created this way working, but I haven't tested that.)

What would totally work though is to createElement("script"), then set .src on it, and stuff it somewhere on your DOM. Not only will it load it and run it, but in many cases, it'll do so asynchronously (vs regular script tags blocking the rest of the page load) which is usually better.



来源:https://stackoverflow.com/questions/4774831/responsetext-contains-javascript-code-and-the-code-doesnt-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!