通过javascript加载jQuery并从另一个文件运行函数
我需要在一个页面中加载3个jquery脚本。出于商业原因,我必须尽可能地减少修改,所以我决定创建一个javascript,将其包含在网站中 在此之后,该脚本必须加载jQuery,并且除了3个css文件外,还必须加载3个jQuery脚本 基本上,直到现在,这就是我得到的:通过javascript加载jQuery并从另一个文件运行函数,javascript,jquery,Javascript,Jquery,我需要在一个页面中加载3个jquery脚本。出于商业原因,我必须尽可能地减少修改,所以我决定创建一个javascript,将其包含在网站中 在此之后,该脚本必须加载jQuery,并且除了3个css文件外,还必须加载3个jQuery脚本 基本上,直到现在,这就是我得到的: var jquery = document.createElement('script'); jquery.type = "text/javascript"; jquery.src = "common/com/jquery/jq
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");
我试图执行其中一个脚本中包含的jquery函数,但没有成功。返回以下错误:
Uncaught ReferenceError: lelakApp is not defined
(其中lelakApp来自我的脚本)
如何加载jQuery和个人jQuery脚本,以及如何从该个人脚本加载函数?
编辑 我正在尝试使用requireJs
define(["../com/jquery/jquery-1.9.1"], function($) {
$('body').html('');
});
但我收到了这个错误:
Uncaught TypeError: undefined is not a function
我认为这是因为您在尝试使用该函数时不知道脚本是否已加载 您可以尝试将onload事件处理程序添加到脚本标记中,并且只在脚本全部加载后才开始使用它们 查看require.js以使用或获取想法:
在使用
lelakApp
之前,应等待脚本完全加载。我建议您使用动态加载脚本
要在不使用require.js的情况下执行此操作,您可以执行以下操作:
script.onload = script.onreadystatechange = function () {
'use strict';
if (script.readyState === 'complete') {
// ...
}
}
注意:我不知道浏览器兼容性。请提供上下文,显示引用lelakApp并抛出此代码的代码error@D-Rock我在代码的末尾添加了我的函数调用。以下函数应返回警报。如果我将所有脚本手动包含在head Tagsorry中,它就会起作用,因为英语不是我的第一语言。我不明白你在评论中的意思。你能给我解释一下吗(也许用这个define()的例子)?没关系。这是我犯的一个错误。现在工作很好,谢谢!
script.onload = script.onreadystatechange = function () {
'use strict';
if (script.readyState === 'complete') {
// ...
}
}