用尽可能少的字符注入 JavaScript 文件?
我正在开发一个小书签,并认为我应该提出一个挑战:如何以尽可能少的字符从链接注入外部 javascript 文件。
这是我能想到的最短的:
javascript:(function(d){d.body.appendChild(d.createElement('script')).src='URL'})(document)
即 88 个字符(不含 URL)。
这里的 Stack Overflow javascript 专家能做得更好吗? 我将接受用最少字符的工作答案,所以请发挥你的思维能力!
(有一件事:小书签必须在所有主要浏览器中工作。这是一个聪明的解决方案,但不起作用在所有主要浏览器中,因为它返回一个值。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
79 个字符。 感谢 Ben Blank 使用
void
。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:
Aside from that, however, your implementation looks pretty minimalist.
假设 String.prototype 没有被污染,我们可以保存一些字符。
Assuming that String.prototype isn't contaminated, we can save some chars.