responseText contains javascript code and the code doesn't load

672 views Asked by At

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

There are 1 answers

1
Metal On

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.