0

I need to append embed code with dynamic variables: http://www73.zippyshare.com/v/57510152/file.html

var c = '<script type="text/javascript">var zippywww="73";var zippyfile="57510152";var zippytext="#000000";var zippyback="#e8e8e8";var zippyplay="#ff6600";var zippywidth=850;var zippyauto=false;var zippyvol=80;var zippywave = "#000000";var zippyborder = "#cccccc";</script><script type="text/javascript" src="http://api.zippyshare.com/api/embed_new.js"></script>';

$("body").html(c);

I've seen solutions using getScript and document.createElement("script") but I receive the embed code dynamically via an API.

  • I could use getScript for the embed_new.js
  • I could possibly write the vars to the current DOM.
  • how to place the output into a desired position then?

Test: http://jsfiddle.net/y658cb9x/

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Martin
  • 2,007
  • 6
  • 28
  • 44
  • 2
    You can't write `` inside a script tag, it closes the current script tag. – adeneo Dec 17 '14 at 21:12
  • 2
    ↑↑↑ so escape it in some way, e.g: `var c = ' – A. Wolff Dec 17 '14 at 21:16
  • Thanks escaping works but then the next problem occurs with embed.js: "Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened." > http://jsfiddle.net/y658cb9x/1/ – Martin Dec 17 '14 at 21:42

1 Answers1

0

Create the script with e.g. jQuery like

$("<script />", {
 html: "Your HTML"
}).appendTo("body");

since you can not create a script-block in a JavaScript block. The string will be parsed like native HTML.

dude
  • 5,678
  • 11
  • 54
  • 81
  • OK: http://jsfiddle.net/y658cb9x/3/ but the same problem as above: "Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened." – Martin Dec 17 '14 at 22:16
  • @Martin you can declare you variables, e.g. zippywidth in your native JavaScript. Because the variables will be global, it will be readable in your source file that you will append with this method above. – dude Dec 18 '14 at 22:25
  • Also see this post: http://stackoverflow.com/questions/10223184/write-javascript-variable-on-html-body – dude Dec 18 '14 at 22:25