简体   繁体   English

通过javascript加载jQuery并从另一个文件运行函数

[英]Load jQuery by javascript and run a function from another file

I need to load 3 jquery scripts in a page. 我需要在页面中加载3个jquery脚本。 For business reasons i've to make the minimum modifications possible so i've decided to create one javascript to be included in the website. 出于业务原因,我必须进行尽可能小的修改,因此我决定创建一个要包含在网站中的JavaScript。

After that, this script must load the jQuery and also 3 more jquery scripts besides 3 css files. 之后,此脚本必须加载jQuery以及3个CSS文件之外的其他3个jquery脚本。

Basically, until now, that's what i've got: 基本上,到现在为止,这就是我所拥有的:

var jquery = document.createElement('script');
jquery.type = "text/javascript";
jquery.src = "common/com/jquery/jquery-1.9.1.js";
document.getElementsByTagName('head')[0].appendChild(jquery,document.getElementsByTagName('head')[0].firstChild);

var prettyPhoto = document.createElement('script');
prettyPhoto.type = "text/javascript";
prettyPhoto.src = "common/com/prettyPhoto/jquery.prettyPhoto.js";
document.getElementsByTagName('head')[0].appendChild(prettyPhoto,document.getElementsByTagName('head')[0].firstChild);

var jqueryui = document.createElement('script');
jqueryui.type = "text/javascript";
jqueryui.src = "common/com/jqueryui/jquery-ui.js";
document.getElementsByTagName('head')[0].appendChild(jqueryui,document.getElementsByTagName('head')[0].firstChild);

var kohlerApp = document.createElement('script');
kohlerApp.type = "text/javascript";
kohlerApp.src = "common/js/lelak.js";
document.getElementsByTagName('head')[0].appendChild(kohlerApp,document.getElementsByTagName('head')[0].firstChild);

var style = document.createElement("link");
style.type = "text/css";
style.href = "common/css/style.css";
style.rel = "stylesheet";
document.getElementsByTagName('head')[0].appendChild(style,document.getElementsByTagName('head')[0].firstChild);

var jqueryui = document.createElement("link");
jqueryui.type = "text/css";
jqueryui.href = "common/css/jquery-ui.css";
jqueryui.rel = "stylesheet";
document.getElementsByTagName('head')[0].appendChild(jqueryui,document.getElementsByTagName('head')[0].firstChild);

var prettyPhoto = document.createElement("link");
prettyPhoto.type = "text/css";
prettyPhoto.href = "common/css/prettyPhoto.css";
prettyPhoto.rel = "stylesheet";
document.getElementsByTagName('head')[0].appendChild(prettyPhoto,document.getElementsByTagName('head')[0].firstChild);

lelakApp.giveError("0","test");

I'm trying to execute one of jquery functions contained in one of the scripts included but with no success. 我正在尝试执行包含的脚本之一中包含的jquery函数之一,但没有成功。 Returns the following error: 返回以下错误:

Uncaught ReferenceError: lelakApp is not defined 

(Where lelakApp is from my script) (其中lelakApp来自我的脚本)

How can i load the jQuery and a Personal Jquery Script and load a function from that personal script, together? 我如何才能同时加载jQuery和个人Jquery脚本并从该个人脚本中加载功能?


EDIT 编辑

I'm trying to execute a simple request of jquery using requireJs 我正在尝试使用requireJs执行一个简单的jquery请求

define(["../com/jquery/jquery-1.9.1"], function($) {
$('body').html('');
});

but i'm receiving this error: 但我收到此错误:

Uncaught TypeError: undefined is not a function

I would think this is because you dont know whether the script has loaded or not at the time you try to use the function. 我认为这是因为您在尝试使用该功能时不知道脚本是否已加载。

You could try adding an onload event handler to the script tags and only start using the scripts once they are all loaded. 您可以尝试将onload事件处理程序添加到脚本标签,并且仅在脚本全部加载后才开始使用脚本。

Take a look at require.js either to use or for ideas : http://requirejs.org/ 看一看require.js可以使用还是参考一下: http ://requirejs.org/

require(["helper/util"], function(util) {
    //This function is called when scripts/helper/util.js is loaded.
    //If util.js calls define(), then this function is not fired until
    //util's dependencies have loaded, and the util argument will hold
    //the module value for "helper/util".
});

You should wait for the scripts to be loaded completely before using lelakApp . 在使用lelakApp之前,您应该等待脚本完全加载。 I suggest you to use require.js for loading scripts dynamically. 我建议您使用require.js动态加载脚本。

in order to do this without require.js , you can do something like: 为了在没有require.js的情况下执行此操作 ,可以执行以下操作:

script.onload = script.onreadystatechange = function () {
    'use strict';

    if (script.readyState === 'complete') {
        // ...
    }
}

Note: I have no ideas about browser compatibility. 注意:我对浏览器兼容性没有任何想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM