我有一个jsp页面DEMO1.jsp,其中我编写了ajax代码来刷新每1 minutes.In DEMO1.JSP
<head>
<script type='text/javascript'>
setInterval(function(){
document.getElementById('bgframe').contentWindow.myInternalFunction();
}, 5*1000);
</script>
</head>
<body onload="AutoRefreshValid();" style="margin:1px;font-family: Serif, Arial,Times, serif;" id="ValidWaybills"><center>
<iframe id='bgframe' style='display:none;' src='DEMO2.jsp'></iframe>
<script type="text/javascript">
function AutoRefreshValid(){
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("No AJAX");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById('ValidWaybills1Valid').innerHTML=xmlHttp.responseText;
setTimeout('AutoRefreshValid()',5*1000);
}
}
xmlHttp.open("GET","DEMO2.jsp",true);
xmlHttp.send(null);
}
</script>
<div id="ValidWaybills1Valid">
</div>
我有DEMO2.JSP,在这里我编写了JAVASCRIPT,我必须在DEMO1.JSP中使用AJAX运行该JAVASCRIPT,而不需要每5秒重新加载页面
<head>
<script type="text/javascript">
function callme3(){
<% Date d1 = new Date();
%>
alert("Date is <%=d1%>");
}
</script>
</head>
<body onload="callme3()">
</div>
感谢和雷加苏达山
发布于 2012-11-10 07:34:33
XML Http请求是关于发送某些内容并检索某些内容的。执行检索到的数据不是其工作的一部分。
以下是您的问题的天真/简单的解决方案,但不建议您这样做。
xmlHttp.open("GET", "SU3.jsp", true);
xmlHttp.onload = function(evt) {
var div = document.createElement('div');
div.innerHTML = evt.target.responseText;
document.body.appendChild(div);
};
xmlHttp.send(null);经过很长一段时间,这段代码最终会使浏览器崩溃。
正确的方法是使SU3.jsp返回json或纯html中的数据,并将所有逻辑移到SU2.jsp。
根据您提供的最新信息(应该在第一个版本中提供),解决方案非常简洁。
在Su2.jsp上,添加以下代码:
// assume map refers to the map object.
// and I use jQuery here to make the point clear,
// it's ok to use XmlHttpRequest directly.
var timer = null;
function updateMarker() {
$.get('su3.jsp', function(data) {
for (var i = 0; i < data.length; i++) {
var datum = data[i];
new google.maps.Marker({
map: map,
position: new google.maps.LatLng(datum.pos.lat, datum.pos.lng),
title: datum.title
});
}
// after all jobs are done, set timeout for another update.
// use setTimeout() instead of setInterval() to tolerate net latency.
timer = setTimeout(updateMaker, 5000);
});
}
// this should be called in onLoad() handler.
updateMaker();在SU3.jsp上,您应该使用Content-Type: application/json输出数据。它输出制造者信息如下:
[{"pos":{"lat":-25.363882,"lng":131.044922},"title":"Australia"}]发布于 2012-11-10 08:31:07
如果您只想从SU3.jsp执行javascript,可以通过SU2.jsp中隐藏的iframe调用它。
将以下JavaScript/标记添加到SU2.jsp
<html>
<head>
<script type='text/javascript'>
setInterval(function(){
document.getElementById('bgframe').contentWindow.location.reload();
},60000); // reload every 60 seconds
</script>
</head>
<body>
<iframe id='bgframe' style='display:none;' src='SU3.jsp'></iframe>
</body>
</html>请记住,如果SU3.jsp需要访问其父函数/变量(SU2.jsp),则需要使用window.parent作为解释的这里
另一方面,如果您只需要每60秒在SU3.jsp内调用一个JavaScript函数,就可以这样做:
document.getElementById('bgframe').contentWindow.myInternalFunction();
setInterval(function(){
document.getElementById('bgframe').contentWindow.myInternalFunction();
}, 60000);https://stackoverflow.com/questions/13320136
复制相似问题