在jsp include多个页面时使用$(document).ready

今天要在一个旧系统上添加一些新功能,有些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以下

转载于:https://my.oschina.net/LinBandit/blog/40314

<%@ include file = "jsp/utils/menu.jsp" %> <%@ include file = "jsp/static/setMenu.js" %> <%@ include file = "jsp/utils/baseUrl.jsp" %> // 用户登出 <%@ include file = "jsp/static/logout.jsp" %> $(document).ready(function () { //我的后台,session信息转移 if (window.localStorage.getItem("Token") != null && window.localStorage.getItem("Token") != 'null') { if (window.sessionStorage.getItem("token") == null || window.sessionStorage.getItem("token") == 'null') { window.sessionStorage.setItem("token", window.localStorage.getItem("Token")); window.sessionStorage.setItem("role", window.localStorage.getItem("role")); window.sessionStorage.setItem("accountTableName", window.localStorage.getItem("sessionTable")); window.sessionStorage.setItem("username", window.localStorage.getItem("adminName")); } } $('.dropdown-toggle .hidden-xs').html(window.sessionStorage.getItem('username')) $('.copyright').html('欢迎使用' + projectName) var token = window.sessionStorage.getItem("token"); if (token == "null" || token == null) { alert("请登录后再操作"); window.location.href = ("jsp/login.jsp"); } setMenu(); if(window.sessionStorage.getItem('role') != '管理员'){ var accountTableName = window.sessionStorage.getItem('accountTableName'); $('#myinfo').attr('href', baseUrl + 'jsp/modules/' + accountTableName + '/add-or-update.jsp'); http(accountTableName+'/session','GET',{},(res)=>{ if(res.code == 0){ window.sessionStorage.setItem('id',res.data.id); window.sessionStorage.setItem('onlyme',true); } }); } });
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值