javascript - How to document.write image hyperlink inside getjson? -


hi want document.write hyperlink image inside getjson tried following doesnt work. guys tell me wrong document write?

<script>   $.getjson('http://anyorigin.com/get?url=http://www.somesite.com/handelit.ashx&callback=?', function(data){       var sitecontents = data.contents;             //writes textarea         document.myform.outputtext.value = sitecontents ;         document.write("<a id="ok" href="http://www.mysite.com/master.m3u8?+sitecontents+"><img src="./playicon.jpg"></a>");      });   </script>  

hi want document.write hyperlink image inside getjson

you can't (not reasonably*). document.write works during initial parsing of page. if use after page finishes loading, replaces page.

instead, interact dom. several ways that, obvious based on code have anchor initially-hidden , show after filling in text area this:

$("#ok").show(); 

full example: live copy | live source

(i've changed playicon.jpg gravatar, since otherwise shows broken image on jsbin)

<!doctype html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <meta charset=utf-8 /> <title>js bin</title> </head> <body>   <form name="myform">     <textarea name="outputtext"></textarea>   </form>   <a id="ok" style="display: none" href="http://www.mysite.com/master.m3u8?+sitecontents+"><img src="http://www.gravatar.com/avatar/f69cfb4677f123381231f97ea1138f8a?s=32&d=identicon&r=pg"></a>   <script>     (function($) {       $.getjson('http://anyorigin.com/get?url=http://www.somesite.com/handelit.ashx&callback=?', function(data){         var sitecontents = data.contents;               //writes textarea           document.myform.outputtext.value = sitecontents;         // shows link         $("#ok").show();       });       })(jquery);   </script> </body> </html> 

* "not reasonably": if content coming same origin document (it doesn't is), synchronous ajax call. bad design.


Comments