Include a JavaScript script with JavaScript

2

1

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!

Adi

Posted 2016-05-24T20:54:36.300

Reputation: 147

3

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 – 2016-05-24T21:04:33.987

quite broad.... – Bald Bantha – 2016-05-24T21:08:24.833

1@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 – 2016-05-24T22:00:34.247

@FryAmTheEggman This question is pretty trivial... – ericw31415 – 2016-05-25T06:51:26.910

Can we assume the page is using jQuery? One answer is assuming so, but I think this is a loophole.

– user48538 – 2016-05-25T10:37:19.263

1I still think this isn't a good question. Currently I'm winning, because I could register a very short domain. – Bálint – 2016-05-25T10:50:59.530

@Bálint You're not winning because your answer isn't valid according to the criteria mentioned in the question. Check my comment under your answer. Also, numerous answers on this site include short domains and shortened URLs, so it seems to be an accepted practice. – Adi – 2016-05-25T10:54:51.763

Could you please clarify if the jQuery answer is valid? The poster seems to interpret the current page as this page, which differs from my interpretation. – Dennis – 2016-05-25T16:57:53.497

@Dennis I think your interpretation was indeed my original intention. I included that explicitly now. I hadn't anticipated that code-golfing involves using semantic loopholes in the wording of the question. I guess my next question would definitely be better. – Adi – 2016-05-25T18:43:16.020

You accepted the answer so early!?! You should have given it at least 4 or 5 days or a whole week! – Arjun – 2016-05-26T00:06:24.097

Downvoting because the challenge requires the use of sketchy link shorteners. – 12Me21 – 2018-05-21T19:31:33.027

Answers

8

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.

Neil

Posted 2016-05-24T20:54:36.300

Reputation: 95 035

4createElement("script") can become createElement\script`` – jrich – 2016-05-24T22:03:19.867

1The with(document) is ingenious. I did not think of that! – ericw31415 – 2016-05-25T00:10:19.693

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 – 2016-05-25T05:42:59.477

1Use this url instead: v.ht/Xa33 – Bálint – 2016-05-25T06:46:13.477

@jrich I think your tip would make a great addition here https://codegolf.stackexchange.com/questions/2682/tips-for-golfing-in-javascript

– Adi – 2016-05-25T07:53:51.933

1

@Adi http://codegolf.stackexchange.com/a/52204/17602

– Neil – 2016-05-25T08:04:03.910

8

49 bytes

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

no one clearly said: you should keep current page content

Nainemom

Posted 2016-05-24T20:54:36.300

Reputation: 81

3

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.

ericw31415

Posted 2016-05-24T20:54:36.300

Reputation: 2 229

OK, I've done it. – ericw31415 – 2016-05-28T00:01:24.153

1

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";

yun o

Posted 2016-05-24T20:54:36.300

Reputation: 11

1

This is only trivially different from the current winning submission, and it's longer.

– Mego – 2016-07-22T18:59:02.097

0

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

jrich

Posted 2016-05-24T20:54:36.300

Reputation: 3 898

If you're going to limit yourself to Firefox, n=[x for(x in document)] works in Firefox 45 and earlier. – Neil – 2016-05-25T07:35:45.497

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 – 2016-05-25T21:18:38.557