我试图动态地将javascript文件包含到我的js文件中.我做了一些关于它的研究,发现jQuery $ .getScript()方法将是一个理想的方法.
// jQuery
$.getScript('/path/to/imported/script.js', function()
{
// script is now loaded and executed.
// put your dependent JS here.
// what if the JS code is dependent on multiple JS files?
});
Run Code Online (Sandbox Code Playgroud)
但我想知道这种方法是否可以一次加载多个脚本?为什么我问这是因为有时我的javascript文件依赖于多个js文件.
先感谢您.
如何将一个<script src="https://remote.com/"></script>元素注入到我的页面中,等待它执行,然后使用它定义的函数?
仅供参考:在我的情况下,脚本会在极少数情况下进行一些信用卡处理,所以我不想总是包含它.我想在用户打开更改信用卡选项对话框时快速包含它,然后向其发送新的信用卡选项.
编辑以获取更多详细信息:我无权访问远程脚本.
有没有办法像同步XMLHttpRequest一样以同步方式加载和执行javascript文件?
我目前正在使用同步XMLHttpRequest,然后使用eval,但调试该代码非常困难......
谢谢你的帮助!
更新
我现在试过这个:
的test.html
<html>
<head>
<script type="text/javascript">
var s = document.createElement("script");
s.setAttribute("src","script.js");
document.head.appendChild(s);
console.log("done");
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
的script.js
console.log("Hi");
Run Code Online (Sandbox Code Playgroud)
输出:完成嗨
所以它没有同步执行.任何想出"嗨"的想法首先出现?
更新2 其他示例
test.html(脚本标记内的代码)
var s = document.createElement("script");
s.setAttribute("src","script.js");
document.head.appendChild(s);
SayHi();
Run Code Online (Sandbox Code Playgroud)
的script.js
function SayHi(){
console.log("hi");
}
Run Code Online (Sandbox Code Playgroud)
输出:未捕获的ReferenceError:未定义SayHi
我正在编写一个需要广泛使用getScript的引擎.为了便于使用,我已将其推入自己的功能,但现在我需要确保函数本身是同步的.不幸的是,我似乎无法让getScript等到它加载的脚本在继续之前实际完成加载.我甚至尝试在调用之前将jQuery的ajax asynch属性设置为false.我正在考虑使用jQuery的when/done协议,但我似乎无法理解将其置于函数内并使函数本身同步的逻辑.任何帮助将非常感谢!
function loadScript(script){
//Unrelated stuff here!!!
$.when(
$.getScript(script,function(){
//Unrelated stuff here!!!
})).done(function(){
//Wait until done, then finish function
});
}
Run Code Online (Sandbox Code Playgroud)
循环代码(按要求):
for (var i in divlist){
switch($("#"+divlist[i]).css({"background-color"})){
case #FFF:
loadScript(scriptlist[0],divlist[i]);
break;
case #000:
loadScript(scriptlist[2],divlist[i]);
break;
case #333:
loadScript(scriptlist[3],divlist[i]);
break;
case #777:
loadScript(scriptlist[4],divlist[i]);
break;
}
}
Run Code Online (Sandbox Code Playgroud) 我想通过代码同步包含来自不同域的JavaScript文件.这意味着使用同步XMLHttpRequest将无法正常工作.我也想避免,document.write因为我的代码将在文档完全加载时执行.这甚至可能吗?是否有任何现有的JavaScript库支持该功能?
基本上我希望这个工作:
<script type="text/javascript">
$(document).ready(function() {
load("path_to_jQuery_UI_from_another_domain");
console.log(jQuery.ui.version); //outputs the version of jQuery UI
});
</script>
Run Code Online (Sandbox Code Playgroud)
编辑:
我的想法是创建一个jQuery插件,根据启用的功能加载其JavaScript文件.jQuery插件可以随时初始化,这意味着没有document.write.完全可以异步加载JavaScript文件,但人们希望他们的插件在调用后完全初始化$("selector").something();.因此需要在没有document.write的情况下加载同步JavaScript.我想我只是想要太多.
我正在尝试动态加载phonegap javascript文件(这样我可以选择在我使用Ripple时不在调试模式下加载它)但我正在遇到一些问题.
我使用普通的脚本标记加载jquery和jquerymobile javascript库.在另一个脚本块中,我做:
function onDeviceReady() {
alert("Device Ready!");
}
$(document).ready(function() {
alert("doc ready!");
$.getScript("js/phonegap.0.9.5.1.js", function() {alert("Got Phonegap!");});
document.addEventListener("deviceready", onDeviceReady, false);
});
Run Code Online (Sandbox Code Playgroud)
此代码提醒它"获得Phonegap!" 但从不警告"设备就绪".使用jsconsole.com,我可以看到PhoneGap javascript对象存在.但是,尝试调用device.uuid(或其他简单的phonegap API调用)失败.这几乎就像PhoneGap没有完全初始化.看起来不应该是这种情况.我错过了什么吗?谢谢!
我正在尝试构建一个简单的Bookmarklet,它将一些外部Javascript加载到一个页面中,目标是将Stackoverflow之类的降价编辑器添加到另一个站点.我有它工作但它需要我点击我的Bookmarklet按钮2次而不是1次.
第一次单击就会出现此错误...
Uncaught ReferenceError: Markdown is not defined init.js:46
Run Code Online (Sandbox Code Playgroud)
第46行是......
var converter1 = Markdown.getSanitizingConverter();
Run Code Online (Sandbox Code Playgroud)
现在第二次点击我的Bookmarklet之后,一切都运行得很好但是第一次点击总是会出现错误并且什么都不做.
这是Bookmarklet文件的代码,请帮我修复,我的Javascript技能不是太好.
加载外部JS和CSS文件
function loadScripts(scriptURL) {
var scriptElem = document.createElement('SCRIPT');
scriptElem.setAttribute('language', 'JavaScript');
scriptElem.setAttribute('src', scriptURL);
void(document.body.appendChild(scriptElem));
}
// Load these 3 Javascript files into the page
// jQuery is already loaded into the page being used so no need to load it
loadScripts('http://codedevelopr.com/labs/javascript/forrst/Markdown.Converter.js');
loadScripts('http://codedevelopr.com/labs/javascript/forrst/Markdown.Sanitizer.js');
loadScripts('http://codedevelopr.com/labs/javascript/forrst/Markdown.Editor.js');
// Load the CSS file into the Page
var head = document.getElementsByTagName('head')[0];
$(document.createElement('link')).attr({
type: 'text/css',
href: 'http://codedevelopr.com/labs/javascript/forrst/demo.css',
rel: 'stylesheet'
}).appendTo(head);
Run Code Online (Sandbox Code Playgroud)
加载文件后我们运行这个...
// …Run Code Online (Sandbox Code Playgroud) javascript ×7
jquery ×4
ajax ×1
cordova ×1
getscript ×1
html ×1
load ×1
mobile ×1
promise ×1
synchronous ×1