使用javascript函数在页面初始化后加载jquery
load jquery after page initialization using javascript function
我正在进行一个项目,为现有的专属门户提供facebook认证。这个专属门户有一个自定义模板的选项,所以…我可以编辑html代码来显示我自己的代码。
问题是,由于某种原因,每次我在像这样的模板上导入脚本时
<script src="external http or https script"></script>
它失败了,结果是一个空白页面,没有从模板加载任何内容。。。
我找到了一个解决方案,在页面加载后导入脚本。。。。
var script=document.createElement('script');
script.src="http://code.jquery.com/jquery-latest.min.js";
var head=document.getElementsByTagName('head')[0]
head.appendChild(script);
它正在工作。。。我可以在控制台上确认,例如调用$('body')会返回html的body元素。。所以jquery已经准备好了。。。
我的问题:如果在加载jquery之后,我加载了其他脚本(在这种情况下是angular和socket.io),那么这些脚本会因jquery问题而失败
Uncaught ReferenceError: $ is not defined
我的问题:jquery加载完成后,我该如何加载这些脚本。。。我需要初始化jquery吗?如何使用它?
提前感谢
完整代码
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="expires" content="-1"/>
</head>
<body onload="initialFunction()">
<script>
function initialFunction(){
document.getElementsByName('username')[0].placeholder="Nombre de Usuario"
document.getElementsByName('password')[0].placeholder="Contraseña"
var script=document.createElement('script');
var angularScript=document.createElement('script');
var socketScript=document.createElement('script');
var angularFacebookScript=document.createElement('script');
var appScript=document.createElement('script');
var materializeScript=document.createElement('script');
script.src="http://code.jquery.com/jquery-latest.min.js";
angularScript.src="http://192.168.1.5/js/angular.js";
socketScript.src="http://192.168.1.5:8080/socket.io/socket.io.js"
angularFacebookScript.src="http://192.168.1.5/angular-facebook-master/lib/angular-facebook.js"
appScript.src="http://192.168.1.5/js/app.js"
materializeScript.src="http://192.168.1.5/js/materialize.min.js"
var head=document.getElementsByTagName('head')[0]
var body=document.getElementsByTagName('body')[0]
head.appendChild(script);
body.appendChild(angularScript);
body.appendChild(socketScript);
body.appendChild(angularFacebookScript);
body.appendChild(appScript);
body.appendChild(materializeScript);
}
</script>
<center>
<style>
body {
background-color:#298eea;
}
....some other stuff...
</style>
<div>
<h3>{company_logo}</h3>
<h2>Captive Portal</h2>
<div id="__loginbox"></div>
//here I want to insert the facebook button with an angular controller to pass info to a backend via socket io.... I have already do that before ussing the "normal way"
</div>
</center>
</body></html>
尝试在script
变量处使用onload
事件
script.onload = function() {
console.log(jQuery().jquery);
// do stuff with jQuery script
}
angularScript.onload = function() {
// do stuff with angular script
}
类似于socketScript
、angularFacebookScript
、appScript
、materializeScript
相关文章:
- Jquery在初始页面加载时选中复选框时显示链接的代码
- 由于传输层/meta/connect调用,使用faye/nodejs在Firefox / chrome上的初始页面加载时
- 为什么require.js似乎在初始页面加载时加载了我的所有模块
- jQuery脚本在初始页面加载时未执行
- 忽略Safari's初始页面加载popstate
- 阻止初始页面加载
- 来自 react-redux-universal-hot-hot 示例初学者工具包中的第三方 API 的初始数据加载
- 角度谷歌地图在初始地图加载后运行一次功能
- 在 Fabric.js 中使用远程 Web 字体初始化加载文本
- 如何在初始页面加载时调用 javascript 函数
- 使用浏览器化加载远程脚本
- 仅在第一页上使用 pace.js(初始站点加载)
- 在初始页面加载时加载目标 ajax php 文件
- Phantom 不会初始化/加载 Angularjs 控制器方法
- 由于尚未定义 Ext.getBody() 而导致初始页面加载中的 ExtJS 错误
- 使绑定元素在初始页面加载后填充时显示
- 在初始页面加载时启动计时器.如果他们离开和返回,必须从第一次装载开始计数
- 在初始页面加载时,自定义Javascript没有加载到mvc 4视图中
- 在初始页面加载时通过Ajax加载内容的设计模式
- D3.js双层分区-初始图表加载放大切片