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/

3
  • 2
    You can't write </script> inside a script tag, it closes the current script tag. Commented Dec 17, 2014 at 21:12
  • 2
    ↑↑↑ so escape it in some way, e.g: var c = '<script>...</scr'+'ipt>'; Commented Dec 17, 2014 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." > jsfiddle.net/y658cb9x/1 Commented Dec 17, 2014 at 21:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

OK: 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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.