从一个.js文件引用到另一个.js文件

时间:2012-10-02 18:25:05

标签: javascript jquery

我使用javascript bookmarklet参考extern路径

javascript:(function(){var s = document.createElement('script');s.src = 'http://192.168.0.100/my.js';document.body.appendChild(s);})();

如何创建对my.js文件所在目录中的jQuery.js文件的引用?

我可以将css文件存入此文件吗?

3 个答案:

答案 0 :(得分:1)

编写与my.js相同的代码:

javascript: (function() {
    var s = document.createElement('script');
    s.src = 'http://192.168.0.100/my.js';
    document.body.appendChild(s);

    var s2 = document.createElement('script');
    s2.src = 'http://192.168.0.100/jquery.js';
    document.body.appendChild(s2);

    var l = document.createElement('link');
    l.rel = 'stylesheet';
    l.href = 'http://192.168.0.100/foo.css';
    l.type = 'text/css';
    document.body.appendChild(l);
})();

答案 1 :(得分:1)

javascript:(function(){
    var s = document.createElement('script');
    s.src = 'http://192.168.0.100/my.js';
    document.body.appendChild(s);
    // Additional js file
    s = document.createElement('script');
    s.src = '<jQuery location goes here>';
    document.body.appendChild(s);
    // CSS file
    s = document.createElement('link');
    s.rel = "stylesheet";
    s.href = "<location goes here>";
    s.type = "text/css";
    document.body.appendChild(s);
})();

答案 2 :(得分:0)

我认为这对你的具体要求可能有点过分,但我也想指出require.js的存在,因为人们可能会对以后找到这个问题感兴趣。

简而言之,它的作用是提供一些动态加载页面资源的工具,支持依赖关系和命名空间(尽管你仍然需要包装你的模块,以免它们阻塞全局命名空间)。

这样,即使在(非常大的)应用程序中,您也可以轻松管理依赖关系等。