[英]Call javascript of another page in ajax xmlHttp.open
我有一个jsp页面DEMO1.jsp,其中我编写了ajax代码以每1分钟刷新一次。在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>
感谢和赞美苏达珊
XML Http Request是关于发送某些内容并检索某些内容的。 执行检索到的数据不是其工作的一部分。
这是您的问题的幼稚/简单解决方案,但不建议您这样做。
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"}]
如果您只想从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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.