简体   繁体   中英

responseText contains javascript code and the code doesn't load

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM