问题:
异步加载js文件,然后在执行加载文件的回调之前检查是否加载了dom.
编辑:我们不使用jQuery; 我们使用Prototype.
编辑:为代码示例添加了更多注释.
大家好,
我正在尝试异步加载我的所有js文件,以防止它们阻塞页面的其余部分.但是当脚本加载并调用回调时,我需要知道DOM是否已经加载,所以我知道如何构造回调.见下文:
//load asynchronously
(function(){
var e = document.createElement('script');
e.type = "text/javascript";
e.async = true;
e.src = srcstr;
// a little magic to make the callback happen
if(navigator.userAgent.indexOf("Opera")){
e.text = "initPage();";
}else if(navigator.userAgent.indexOf("MSIE")){
e.onreadystatechange = initPage;
}else{
e.innerHTML = "initPage();";
}
// attach the file to the document
document.getElementsByTagName('head')[0].appendChild(e);
})();
initPageHelper = function(){
//requires DOM be loaded
}
initPage = function(){
if(domLoaded){ // if dom is already loaded, just call the function
initPageHelper(); …
Run Code Online (Sandbox Code Playgroud) 在firefox开发人员工具中,在"Net"面板下,加载的资源将其加载时间拆分为不同的颜色/类别.这些是:
这些中的每一个代表什么,更具体地说,它们中的任何一个是否准确地代表了服务器思考的时间量(访问数据库,运行算法等)?
谢谢.
目标:在调用运行函数的函数范围内声明/设置变量.
详情:
嘿Overflowans,
我想在我的很多php函数中使用的实用程序中添加最后一小块糖.它允许我在我的函数输入上定义一个灵活的契约.经过几次迭代,我已经把它缩减到一个看起来像这样的用法:
function doSomething($param_arr){
FPX::contract(array(
'required' => array("length", "width", "height", "weight"),
'optional' => array("circumference")
));
$length = $parr['length'];
$width = $parr['width'];
$height = $parr['height'];
$weight = $parr['weight'];
$circumference = $parr['circumference'];
....
}
Run Code Online (Sandbox Code Playgroud)
FPX :: contract()自动获取$ param_arr并对其进行解析以确保它与定义的合同兼容.
我现在要做的是消除之后声明每个变量的需要.在最低的函数中,有没有一种方法可以在调用它的函数范围内声明变量?因此FPX :: contract()需要能够设置doSomething()范围内的变量,这样我就不必声明每个变量.(我不想声明全局变量).
理想情况下,它看起来像:
function doSomething($param_arr){
FPX::contract(array(
'required' => array("length", "width", "height", "weight"),
'optional' => array("circumference")
));
....
}
Run Code Online (Sandbox Code Playgroud)
然后doSomething()将能够访问合同中列出的每个变量$ length,$ width等.
我知道函数($ var0,$ var1,$ var2 = null)语法,但是这个语法与大量可选变量一起使用并不是很容易.
谢谢,肯
asynchronous ×1
callback ×1
dom ×1
firebug ×1
firefox ×1
function ×1
javascript ×1
parameters ×1
php ×1
prototypejs ×1
scope ×1