今天要在一个旧系统上添加一些新功能,有些js函数要求要在页面加载完成的时候执行,但是我发现使用jquery的$(document).ready完全没有效果,后来一检查才知道,原来这个jsp中,include了几个其他的jsp页面,而其中一个top.jsp页面已经使用了$(document).ready,所以到了我这里就没用了,这个郁闷啊,因为这个页面有些数据必须在页面初始化的时候加载并显示,真是万分纠结,也闪过将所有需要页面加载时初始化的代码都往top.jsp里面塞的恶心想法。
思前想后,最后用了一个折中的办法,在top.jsp中,定义一个全局函数数组funArray,然后每个include了top.jsp的页面,都将要在页面初始化的时候执行的函数push到funArray里面:
funArray.push(
function(){
alert("i'm a function");
}
)
然后在top.jsp中这样写:
$(document).ready(
function(){
for(var i in funArray){
funArray[i]();
}
}
)
就行了,当然要保证其他页面的script要在top.jsp以下