1

1分ごとに更新するajaxコードを記述したjspページDEMO1.jspがあります.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>

JAVASCRIPT を記述した DEMO2.JSP があります。5 秒ごとにページをリロードせずに、DEMO1.JSP で AJAX を使用してその JAVASCRIPT を実行する必要があります。

<head>


    <script type="text/javascript">
        function callme3(){

        <%  Date d1 = new Date();
        %>
                alert("Date is          <%=d1%>");
            }
    </script>
</head>
<body onload="callme3()">



</div>

ありがとう、スダルシャン

4

2 に答える 2

1

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-10T07:34:33.710 に答える
0

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を使用する必要があることに注意してください。

一方、SU3.jsp 内で 60 秒ごとに JavaScript 関数を呼び出す必要がある場合は、次のように実行できます。

document.getElementById('bgframe').contentWindow.myInternalFunction();

setInterval(function(){
    document.getElementById('bgframe').contentWindow.myInternalFunction();
}, 60000);
于 2012-11-10T08:31:07.243 に答える