0

I am executing this code in my browser (chrome) through the url bar.

document.write('<script src=http://example.com/example.js></script>')

When that is executed on a page, it overwrites it and places:<script src=http://example.com/example.js></script> as the source code of the page and then launches the .js script. My question is, is it possible to launch that .js script without overwriting the entire page with a small amount of code? If so how?

Tom
  • 75
  • 1
  • 1
  • 12
  • @RickyMutschlechner no it is not a duplicate, i cannot use jquery and i want very short code. – Tom Aug 07 '13 at 20:05

1 Answers1

0

Try creating the script element in javascript during the body's onload handler, and then append the script element you created to the document.

fred02138
  • 3,323
  • 1
  • 14
  • 17
  • I know how to do that, but it's too long, i want something very short. – Tom Aug 07 '13 at 20:05
  • @Tom Make it into a function and call it – Ian Aug 07 '13 at 20:07
  • @Tom how is that long..? It's literally two lines, not counting the opening and closing script tag. I'm not sure if it'll work, but it's most certainly not long. – Ricky Mutschlechner Aug 07 '13 at 20:07
  • @RickyMutschlechner It's easy to do the talk, i want to see the walk. – Tom Aug 07 '13 at 20:46
  • @Tom `var script = document.createElement("script"); script.src = "some url"; document.body.appendChild(script);`. And like I said, it can be put into a function that all you end up using is something like `addScript("some url");` – Ian Aug 07 '13 at 22:58
  • @Ian why doesn't `` work? – Tom Aug 08 '13 at 17:01
  • @Tom You don't seem to be using quotes. Try using ``. Although I'd advise against using inline event handlers. It could be extracted and put into `` – Ian Aug 08 '13 at 17:06
  • @Ian could we not use quotes some other way without using string.fromcharcode? – Tom Aug 08 '13 at 17:15
  • @Ian I'm referring to quotes within the actual code, excluding the first and last quotes. – Tom Aug 08 '13 at 17:20