# Include a JavaScript script with JavaScript

To dynamically include a JavaScript file in a web page, you'd normally insert a <script> tag pointing to your script. For example:

var js_file= document.createElement("script");
js_file.src='http://example.com/js.js';
document.body.appendChild(js_file);

Write JavaScript code will load an external JavaScript file in the current page and:

• The file is hosted on https://pastebin.com/raw/rV6dDDj9 (content is console.log('Good job!');)
• The code must be vanilla JavaScript (no jQuery, etc.)
• Answer validity test is done in the web browser console. Criteria:
• Console prints Good job!
• Welcome to PPCG! Unfortunately, I think this challenge has some problems. Are URL shorteners allowed? Do you expect any possible improvement beyond that? I don't know enough about javascript to be sure, but it looks fairly cut-and-dry. Just so you know, we have a Sandbox where you can post to receive feedback on your challenge before posting to the main site. – FryAmTheEggman May 24 '16 at 21:04
• quite broad.... – Bald Bantha May 24 '16 at 21:08
• @FryAmTheEggman Thanks. I originally thought shortners wouldn't be allowed because I specified the exact URL, but I guess it's not an issue since people are using them. @ Bald You're welcome to give specific suggestions on the broad areas so I can try to limit the scope – Adi May 24 '16 at 22:00
• @FryAmTheEggman This question is pretty trivial... – ericw31415 May 25 '16 at 6:51
• I still think this isn't a good question. Currently I'm winning, because I could register a very short domain. – Bálint May 25 '16 at 10:50

## 7775 71 bytes

with(document)body.appendChild(createElementscript).src=//v.ht/Xa33

Edit: Saved 2 bytes thanks to @jrich. Saved 4 bytes thanks to @Bálint.

• createElement("script") can become createElementscript – jrich May 24 '16 at 22:03
• The with(document) is ingenious. I did not think of that! – ericw31415 May 25 '16 at 0:10
• I wouldn't have imagined that the appendChild would return a reference to the appended Node. This alone saved a lot of bytes. Thanks you very much for teaching something new. – Adi May 25 '16 at 5:42
• Use this url instead: v.ht/Xa33 – Bálint May 25 '16 at 6:46
• – Neil May 25 '16 at 8:04

## 49 bytes

document.write<script src=//v.ht/KvtI></script>

no one clearly said: you should keep current page content

# 75 bytes

document.body.appendChild(document.createElementscript).src="//v.ht/Xa33"

@Neil's answer was quite similar to this, but he found an ingenious shortcut involving with(...). I will not copy it here, because he came up with it.

Thanks to @Bálint for the short URL.

• OK, I've done it. – ericw31415 May 28 '16 at 0:01

## 72 bytes

(D=document).body.appendChild(D.createElementscript).src="//v.ht/Xa33"

for using more than once(3 call):

92 then always 36

(D=document)[B="body"][A="appendChild"](D[C="createElement"](s="script")).src="//v.ht/Xa33"; D[B][A](D[C](s)).src="//v.ht/Xa33"; D[B][A](D[C](s)).src="//v.ht/Xa33"

93 then always 35

D=document,B=D.body,B.a=B.appendChild,D.c=D.createElement; B.a(D.cscript).src="//v.ht/Xa33"; B.a(D.cscript).src="//v.ht/Xa33"; B.a(D.cscript).src="//v.ht/Xa33";

99 then always 30

D=document,B=D.body,B.a=B.appendChild,D.c=D.createElement,s="script"; B.a(D.c(s)).src="//v.ht/Xa33"; B.a(D.c(s)).src="//v.ht/Xa33"; B.a(D.c(s)).src="//v.ht/Xa33";

99 then always 26

D=document,A=e=>D.body.appendChild(e),C=e=>D.createElement(e),s="script"; A(C(s)).src="//v.ht/Xa33"; A(C(s)).src="//v.ht/Xa33"; A(C(s)).src="//v.ht/Xa33";

87 then always 15

S=e=>document.body.appendChild(document.createElement("script")).src=e; S//v.ht/Xa33; S//v.ht/Xa33; S//v.ht/Xa33;

88 then always 22

S=e=>document.body.appendChild(document.createElement("script")); S().src="//v.ht/Xa33"; S().src="//v.ht/Xa33"; S().src="//v.ht/Xa33";

• This is only trivially different from the current winning submission, and it's longer. – user45941 Jul 22 '16 at 18:59

## 89 bytes

n=[];for(i in d=document)n.push(i);d.body[n[173]](d[n[41]]script).src=//goo.gl/rPQ0Jk

This is incredibly browser-dependent. Actually even worse than that, it pretty much only works on codegolf.stackexchange.com, in the browser console, in FireFox. Ah well.

Please don't get too mad when it (likely) doesn't work on your particular browser configuration, since if it is even slightly different from mine it could fail.

(yeah, probably invalid because of that. Not going to win anyway.)

• If you're going to limit yourself to Firefox, n=[x for(x in document)] works in Firefox 45 and earlier. – Neil May 25 '16 at 7:35
• In theory this could get it down to 77 bytes, but I don't have that version to test. n=[x for(x in d=document)];d.body[n[173]](d[n[41]]script).src=//v.ht/Xa33 – jrich May 25 '16 at 21:18