当前位置: 代码迷 >> Java Web开发 >> java项目里头javascript打开页面时不执行,必须刷新一下才执行
  详细解决方案

java项目里头javascript打开页面时不执行,必须刷新一下才执行

热度:163   发布时间:2016-04-16 21:36:28.0
java项目里面javascript打开页面时不执行,必须刷新一下才执行
java项目,使用了SSH框架,projectsearch.jsp页面显示项目列表,提交后跳转到typeanalysis.jsp,显示项目下面的具体内容,typeanalysis.jsp里面使用ichartjs做了一个饼图,但是跳转之后,js脚本不执行,必须要刷新一下才能显示,尝试了很多方法,onload方法也使用了,即便写最简单的alert也不能执行
projectsearch.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<title>查询页面</title>
<link rel="stylesheet" href="css/jquery.mobile-1.3.0.min.css" />
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/jquery.mobile-1.3.0.min.js"></script>

</head>
<body>
<form action="typeanalysis" method="get">
<div>
<label for="projectname">项目名称:</label>
<!--<input type="text"  id="fid11" name="projectId" />-->
<select name="projectId">
<%
ArrayList<Object[]> arrayList1 = (ArrayList<Object[]>) request.getAttribute("list");
for (int i = 0; i < arrayList1.size(); i++) {
Object[] object1 = arrayList1.get(i);
String projectName = object1[0].toString(); //项目名称
String projectId = object1[1].toString(); //项目ID
%>
<option value="<%=projectId%>"><%=projectName%></option>
<%
}
%>
</select>
</div>

<footer>
<input type="submit" value="提交" />
</footer>
</form>
</body>
</html>

typeanalysis.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <head>
    <title>户型分析图</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="css/jquery.mobile-1.3.0.min.css" />
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/jquery.mobile-1.3.0.min.js"></script>
    <script src='js/ichart.latest.min.js'></script>
    
<script type='text/javascript'>
$(function(){
      var chart = iChart.create({
            render:"ichart-render",
            width:800,
            height:400,
            background_color:"#f4f4f4",
            gradient:false,
            color_factor:0.2,
            border:{
                  color:"#bcbcbc",
                  width:1
            },
            align:"center",
            offsetx:0,
            offsety:0,
            sub_option:{
                  border:{
                        color:"#6d869f",
                        width:2
                  },
                  label:{
                        fontweight:500,
                        fontsize:18,
                        color:"#4572a7",
                        sign:"square",
                        sign_size:20,
                        border:{
                              color:"#BCBCBC",
                              width:0
                        },
                        background_color:"rgba(244,244,244,0)"
                  }
            },
            shadow:true,
            shadow_color:"#666666",
            shadow_blur:5,
            showpercent:true,
            column_width:"70%",
            bar_height:"70%",
            radius:"90%",
            title:{
                  text:"户型分析图",
                  color:"#6d869f",
                  fontsize:20,
                  textAlign:"center",
                  font:"微软雅黑",
                  height:30,
                  offsetx:0,
                  offsety:0
            },
            subtitle:{
                  color:"#111111",
                  fontsize:16,
                  textAlign:"center",
                  font:"微软雅黑",
                  height:20,
                  offsetx:0,
                  offsety:0
            },
            footnote:{
                  color:"#111111",
                  fontsize:12,
                  textAlign:"right",
                  font:"微软雅黑",
                  height:20,
                  offsetx:0,
                  offsety:0
            },
            legend:{
                  enable:false,
                  background_color:"#fefefe",
                  color:"#333333",
                  fontsize:12,
                  border:{
                        color:"#BCBCBC",
                        width:1
                  },
                  column:1,
                  align:"right",
                  valign:"center",
                  offsetx:0,
                  offsety:0
            },
            coordinate:{
                  width:"80%",
                  height:"84%",
                  background_color:"#ffffff",
                  axis:{
                        color:"#a5acb8",
                        width:[1,"",1,""]
                  },
                  grid_color:"#d9d9d9",
                  label:{
                        fontweight:500,
                        color:"#666666",
                        fontsize:11
                  }
            },
            label:{
                  fontweight:500,
                  color:"#666666",
                  fontsize:11
            },
            type:"pie2d",
            data:[
            
               <% 
    ArrayList<Object[]> arrayList1 = (ArrayList<Object[]>) request.getAttribute("list");
    for(int i=0;i<arrayList1.size();i++){
Object[] object1 = arrayList1.get(i);
String roomNum = object1[0].toString();   //房间数
String roomstatus = object1[1].toString();  //房间状态
String roomstruct = object1[2]!=null?object1[2].toString():"";  //房间结构
String roomtype = roomstatus+roomstruct;   //房间类型
String[] color = new String[] {"rgba(254,221,116,0.9)","rgba(130,216,239,0.9)","rgba(247,104,100,0.9)","rgba(128,189,145,0.9)","rgba(253,159,193,0.9)","rgba(130,221,193,0.9)"};
System.out.println(roomtype+"@@@@@@@@@"+roomNum+"&&&&&&&&&&&&"+color[i]);
%>
{
name:"<%=roomtype%>",
value:<%=roomNum%>,
color:"<%=color[i]%>"
}
<%
if(i!=arrayList1.size()-1){
%>,<%
}
}
%>

            ]
      });
      chart.draw();
});

</script>
  </head>
  
  <body>
    <div id='ichart-render'> </div>
    <script>function refelsh(){location.reload();}</script>
    <button onClick="refelsh();" value="刷新">刷新</button>
  </body>
</html>

------解决思路----------------------
你在body 的时候加上个 onload 试试,页面加载后, 执行 js 方法

<body onload="init()">
</body>
------解决思路----------------------
<body>
<script>
要写在body里
</script>
</body>
------解决思路----------------------
引用:
Quote: 引用:

<body>
<script>
要写在body里
</script>
</body>

已经尝试过了,各种方法都试过了,还是不行


用我这个方法写个 alert 试试能不能行,如果可以的话,就是jquery的加载问题。
------解决思路----------------------
如果页面使用JS比较多,特效也比较多,异步也用到了,可以考虑使用jQuery
------解决思路----------------------
我觉得应该是你 js 代码出了问题吧, 打开 IE debugger 看看
------解决思路----------------------
是不是js太大 
第一次进去没加载完全啊?
看js的问题 我推荐火狐浏览器 
很方便的
------解决思路----------------------
用firefox浏览器调试,F12进入控制器,选择JS,看看是不是其他JS写错了
------解决思路----------------------

在<head></head>标签的<script></script>标签添加如下
$(document).ready(function(){
 //这里直接添加生成图片的js
})

不要在<body>标签添加
<script>function refelsh(){location.reload();}</script>

js不执行大多情况是js代码某地方出错了,仔细检查,一步步排除错误