[英]How do I include external javascript files in bookmarklets?
I'm making a cdn for a bunch of scripts I've written for work. 我正在为一堆为工作编写的脚本制作CDN。 I used to have to distribute them and each person get the file and install it on their computer, but I'm moving them to an amazon cloudfront/s3 instance.
我曾经不得不分发它们,每个人都获取文件并将其安装在他们的计算机上,但是我正在将它们移动到Amazon cloudfront / s3实例中。
I'll be using this to inject the bookmarklet: http://allben.net/post/2010/01/30/CSS-JavaScript-Injection-Bookmarklets 我将使用它来注入小书签: http : //allben.net/post/2010/01/30/CSS-JavaScript-Injection-Bookmarklets
However, the old way I was doing this I used the jquery bookmarklet generator. 但是,我执行此操作的旧方法是使用jquery小书签生成器。 This is different than that and I am unaware how to include jquery should I want to use it.
与此不同,我不知道我是否要使用jquery。
Here is an example script: 这是一个示例脚本:
javascript:(function(){var%20s=document.createElement('script');s.setAttribute('src','cdn.domain.com/scripts/somescript.js');document.getElementsByTagName('body')[0].appendChild(s);})();
That goes in a bookmark. 那在书签中。
The script: 剧本:
alert($(somecontainer).size());
Obviously this doesn't work because the bookmarklet is no longer injecting jquery. 显然,这不起作用,因为bookmarklet不再注入jquery。 So, what is the best way to go about this?
那么,最好的方法是什么?
The problem you are facing, I guess, is that the jQuery bookmarklet generator does not make $
available within the page. 我想,您面临的问题是jQuery小书签生成器无法使
$
在页面内可用。 It keeps the jQuery variable isolated within a function and then removes jQuery entirely from the page after running. 它将jQuery变量隔离在一个函数中,然后在运行后将jQuery从页面中完全删除。
Below is a modified version of code from here: http://benalman.com/projects/run-jquery-code-bookmarklet/ which should work. 下面是来自此处的代码的修改版本: http : //benalman.com/projects/run-jquery-code-bookmarklet/应该起作用。
function (e, a, g, h, f, c, b, d) {
if (!(f = e.jQuery) || g > f.fn.jquery || h(f)) {
c = a.createElement("script");
c.type = "text/javascript";
c.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + g + "/jquery.min.js";
c.onload = c.onreadystatechange = function () {
if (!b && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
var s = document.createElement('script');
s.setAttribute('src', 'cdn.domain.com/scripts/somescript.js');
document.getElementsByTagName('body')[0].appendChild(s)
}
};
a.documentElement.childNodes[0].appendChild(c)
}
})(window, document, "1.3.2")
Keep in mind that this will replace any jQuery
and $
variable on the page. 请记住,这将替换页面上的所有
jQuery
和$
变量。 If you need to run the bookmarklet on pages that use those variables already, use jQuery.noConflict(1)
. 如果需要在已经使用这些变量的页面上运行小书签,请使用
jQuery.noConflict(1)
。 For example _jq = e.jQuery.noConflict(1)
will let you use _jq
instead of $
and will return the original $
and jQuery
to their original values. 例如,
_jq = e.jQuery.noConflict(1)
将使您使用_jq
而不是$
,并将原始的$
和jQuery
返回其原始值。 Example: 例:
alert(_jq(somecontainer).size());
If you want to use noConflict but also use $ in your .js code, wrap your code in a function and create a locally scoped $
. 如果要在.js代码中使用noConflict,但也要使用$,则将代码包装在一个函数中,然后创建一个本地范围的
$
。 Example: 例:
(function(){
var $ = _jq; // this $ will not affect any $ that exists outside this function.
alert($(somecontainer).size());
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.