2
\$\begingroup\$

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:

  • Shortest valid answer wins
  • 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:
    • URL requested and JS file downloaded.
    • Console prints Good job!
\$\endgroup\$
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – FryAmTheEggman May 24 '16 at 21:04
  • \$\begingroup\$ quite broad.... \$\endgroup\$ – Bald Bantha May 24 '16 at 21:08
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ – Adi May 24 '16 at 22:00
  • \$\begingroup\$ @FryAmTheEggman This question is pretty trivial... \$\endgroup\$ – ericw31415 May 25 '16 at 6:51
  • 1
    \$\begingroup\$ I still think this isn't a good question. Currently I'm winning, because I could register a very short domain. \$\endgroup\$ – Bálint May 25 '16 at 10:50
8
\$\begingroup\$

77 75 71 bytes

with(document)body.appendChild(createElement`script`).src=`//v.ht/Xa33`

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

\$\endgroup\$
  • 4
    \$\begingroup\$ createElement("script") can become createElement`script` \$\endgroup\$ – jrich May 24 '16 at 22:03
  • \$\begingroup\$ The with(document) is ingenious. I did not think of that! \$\endgroup\$ – ericw31415 May 25 '16 at 0:10
  • \$\begingroup\$ 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. \$\endgroup\$ – Adi May 25 '16 at 5:42
  • 1
    \$\begingroup\$ Use this url instead: v.ht/Xa33 \$\endgroup\$ – Bálint May 25 '16 at 6:46
  • 1
    \$\begingroup\$ @Adi codegolf.stackexchange.com/a/52204/17602 \$\endgroup\$ – Neil May 25 '16 at 8:04
8
\$\begingroup\$

49 bytes

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

no one clearly said: you should keep current page content

\$\endgroup\$
3
\$\begingroup\$

75 bytes

document.body.appendChild(document.createElement`script`).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.

\$\endgroup\$
  • \$\begingroup\$ OK, I've done it. \$\endgroup\$ – ericw31415 May 28 '16 at 0:01
1
\$\begingroup\$

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.c`script`).src="//v.ht/Xa33"; B.a(D.c`script`).src="//v.ht/Xa33"; B.a(D.c`script`).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";

\$\endgroup\$
  • 1
    \$\begingroup\$ This is only trivially different from the current winning submission, and it's longer. \$\endgroup\$ – Mego Jul 22 '16 at 18:59
0
\$\begingroup\$

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.)

\$\endgroup\$
  • \$\begingroup\$ If you're going to limit yourself to Firefox, n=[x for(x in document)] works in Firefox 45 and earlier. \$\endgroup\$ – Neil May 25 '16 at 7:35
  • \$\begingroup\$ 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` \$\endgroup\$ – jrich May 25 '16 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.