1

I have the following snippet:

<html>
 <head>
 </head>
 <body>
  <form>
   <div id='mydiv'>
   </div>
  </form>
 </body>
</html>

I want to add "recaptcha" inside #mydiv usign a javascript code given at https://developers.google.com/recaptcha/docs/display just before </body>:

I tried to use the following code:

<script>
var mydiv = document.getElementById('mydiv');
var script = document.createElement('script');
script.type = "text/javascript";
script.src = 'http://www.google.com/recaptcha/api/challenge?k=6LeZBs8SAAAAAFnpP1frAONoZH-I-W9HOm0RQgV0';
mydiv.appendChild(script);
</script>

But it doesn't work. It only paste the script code. It doesn't run it.

Do you have any idea how to do this?

Thanks.

Ivan
  • 36
  • 6
  • And did you try anything else? Like placing your code just before instead? – freddy Feb 23 '14 at 21:33
  • i tried with these: http://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml http://stackoverflow.com/questions/2592092/executing-script-elements-inserted-with-innerhtml but i could not find the correct answer. – Ivan Feb 23 '14 at 21:35
  • I want to do it with a script placed at the beginning or at the end of the body. – Ivan Feb 23 '14 at 21:40

1 Answers1

1

Here you go taken from the example code on your link see fiddle,

http://jsfiddle.net/GVwZ4/1/

in a div

http://jsfiddle.net/GVwZ4/2/

<body>
      <script type="text/javascript"
     src="http://www.google.com/recaptcha/api/challenge?k=6LeZBs8SAAAAAFnpP1frAONoZH-I-    W9HOm0RQgV0">
  </script>
    <div></div>
</body>
lpoulter
  • 158
  • 9
  • Is this what you were looking for? – lpoulter Feb 23 '14 at 21:48
  • that is what i want, BUT using a script that is outside of the div – Ivan Feb 23 '14 at 21:57
  • Well that would be dynamically loading the tag why do you want to do this. This is possible if you use an external .js file see here http://stackoverflow.com/questions/3857874/how-to-dynamically-insert-a-script-tag-via-jquery-after-page-load – lpoulter Feb 23 '14 at 22:29
  • Thanks,, but i did it with jQuery. I used created a div with the script first and then .appendTo to move it to mydiv – Ivan Feb 24 '14 at 16:46