JSON对象传递给外部JavaScript - 酷技术 [英] JSON Object passed to External JavaScript - Cool Technique

查看:123
本文介绍了JSON对象传递给外部JavaScript - 酷技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 FireBug Lite ,看到他们使用非常酷的技术将选项传递到外部脚本文件。

I was looking at FireBug Lite and saw that they use a pretty cool technique to pass options into an external script file.

<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js">
{
    overrideConsole: false,
    startInNewWindow: true,
    startOpened: true,
    enableTrace: true
}
</script>

我想知道有没有人知道这种技术的名称,哪里可以找到更多关于它的信息或它如何工作。看起来很酷谢谢!

I was wondering if anyone know of the name of this technique and where I can find more info about it or how it works. Seems pretty cool. Thanks!

推荐答案

这不是一个自动变量传递技术,你可能会想。

It's not an automatic variable-passing-technique as you may think.

所有的代码都是循环遍历所有的脚本标签,直到找到加载代码的代码(通过比较 src 属性到正则表达式( /(firebug-lite(?: - \w +)?(?: \.js | \.jgz))(? #(。+))?$ /; )。

All their code does is loop through all the script tags until they find the one which loaded their code (by comparing the src attribute to a regular expression (/(firebug-lite(?:-\w+)?(?:\.js|\.jgz))(?:#(.+))?$/;).

如果找到标签,只需得到 脚本标签中的.innerHTML ,并对其进行评估。

If it finds the tag, it simply gets the .innerHTML of the script tag, and evaluates it.

我猜这个未命名)技术在现实世界中无关,因为我们没有一种有保证的方法来查找哪些脚本标记指向我们的库(特别是它是常见的所有脚本都可以被组合成一个脚本文件在实时服务器上)。

I guess this (unnamed) techique isn't relevant in the real-world, as we don't have a guaranteed method of finding which script tag refers to our library (especially as it is common for all script's to be combined into one script file on live servers).

此外,我怀疑跨浏览器是如何的,因为它肯定不通过 spec ,其中指出:

Furthermore, I have my doubts over how cross-browser this is; as it certainly doesn't go by the spec, which states:

并考虑过): spec 指出浏览器不应该解释两者。然而,这与这种技术无关。浏览器不需要插入,因为通过 innerHTML 读取脚本的内容(即使它在内容中读取,它也不会任何伤害无论如何)。 Aslong浏览器符合规范,并加载URI(所有浏览器都做),没有问题! (除了不知道/保证您的图书馆所属的脚本标记)。

Having said that (and thought about it): the spec states that the browser shouldn't interpret both. However this isn't relevant with this technique. The browser doesn't have to interpet both, as the content of the script is read in through innerHTML (and even if it did read in the content, it doesn't do any harm anyway). Aslong as the browser conforms to the spec, and loads the URI (which all browsers do), there's no problem! (apart from not knowing/ guaranteeing which script tag your library belongs to).


脚本可以在SCRIPT元素的
内容中定义,也可以在
中定义为外部文件。如果src属性
未设置,则用户代理必须将
的元素的内容解释为
脚本。如果src具有URI值,
用户代理必须忽略该元素的
内容,并通过
的URI来检索脚本。

The script may be defined within the contents of the SCRIPT element or in an external file. If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.

(即不解释两者)。

这篇关于JSON对象传递给外部JavaScript - 酷技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆