Question

I'm working on a bookmarklet, and thought I'd throw down a challenge: how to inject an external javascript file from a link in as few characters as possible.

Here's the shortest I was able to come up with:

javascript:(function(d){d.body.appendChild(d.createElement('script')).src='URL'})(document)

That's 88 characters without the URL.

Can the Stack Overflow javascript gurus here do better? I'll be accepting the working answer with the fewest characters, so put on your thinking caps!

(One thing: the bookmarklet must work in all major browsers. This is a clever solution, but doesn't work in all major browsers, because it returns a value.)

Was it helpful?

Solution

Assuming that String.prototype isn't contaminated, we can save some chars.

javascript:with(document)(body.appendChild(createElement('script')).src='URL')._

OTHER TIPS

javascript:void(with(document)body.appendChild(createElement('script')).src='URL')

79 characters. Credit to Ben Blank for the use of void.

I'm not sure why you're wrapping this in a function enclosure — it seems to work perfectly well without and is almost a dozen characters shorter:

javascript:void(document.body.appendChild(document.createElement('script')).src='URL')

Aside from that, however, your implementation looks pretty minimalist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top