-1

誰かが私が間違っていることを教えてくれることを望んでいました。Google チャートを xmlhttp で更新したいのですが、私のスクリプトは次のとおりです。

<!--
You are free to copy and use this sample in accordance with the terms of the
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html)
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
    <div id="myDiv">
    <script type="text/javascript">
      function drawVisualization() {
        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Austria', 'Belgium', 'Czech Republic', 'Finland', 'France', 'Germany'],
          ['2003',  1336060,   3817614,       974066,       1104797,   6651824,  15727003],
          ['2004',  1538156,   3968305,       928875,       1151983,   5940129,  17356071],
          ['2005',  1576579,   4063225,       1063414,      1156441,   5714009,  16716049],
          ['2006',  1600652,   4604684,       940478,       1167979,   6190532,  18542843],
          ['2007',  1968113,   4013653,       1037079,      1207029,   6420270,  19564053],
          ['2008',  1901067,   6792087,       1037327,      1284795,   6240921,  19830493]
        ]);

        // Create and draw the visualization.
        new google.visualization.ColumnChart(document.getElementById('visualization')).
            draw(data,
                 {title:"Yearly Coffee Consumption by Country",
                  width:600, height:400,
                  hAxis: {title: "Year"}}
            );
      }




    google.setOnLoadCallback(drawVisualization);
    ;
    </script>
    </div>

    <script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","UpdateGraph.php",true);
xmlhttp.send();
}
</script>

  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="visualization" style="width: 600px; height: 400px;"></div>



<button type="button" onclick="loadXMLDoc()">Change Content</button>


  </body>
</html>

以下は、コンテンツを更新したい php ファイル (UpdateGraph.php) です。私の意図は、2 番目のグラフが表示されることです。現在の動作方法は、別のグラフを表示するスペースと、自動的に「再描画」されない場合に備えて作成したボタンです。UpdateGraph.php ファイルからスクリプト タグを削除すると、新しいグラフがあるはずの領域にテキストだけが表示されます。

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load('visualization', '1', {packages: ['corechart']});

</script>
<script type="text/javascript">
      function drawVisualization2() {
        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Austria', 'Belgium', 'Czech Republic', 'Finland', 'France', 'Germany'],
          ['2003',  1336060,   3817614,       974066,       1104797,   6651824,  15727003],
          ['2004',  1538156,   3968305,       928875,       1151983,   5940129,  17356071],
          ['2005',  1576579,   4063225,       1063414,      1156441,   5714009,  16716049],
          ['2006',  1600652,   4604684,       940478,       1167979,   6190532,  18542843],
          ['2007',  1968113,   4013653,       1037079,      1207029,   6420270,  19564053],
          ['2008',  1901067,   6792087,       1037327,      1284795,   6240921,  19830493]
        ]);

        // Create and draw the visualization.
        new google.visualization.ColumnChart(document.getElementById('visualization2')).
            draw(data,
                 {title:"Yearly Coffee Consumption by Country",
                  width:600, height:400,
                  hAxis: {title: "Year"}}
            );
      }
      google.setOnLoadCallback(drawVisualization2)
</script>
<button onclick="drawVisualization2()">Redraw 2nd Graph</button>
<div id="visualization2" style="width: 600px; height: 400px;"></div>

テキストの取り込みに問題はありませんが、新しい JavaScript を取り込んだり、JavaScript を更新したりできない理由がわかりません。(ちなみに、UpdateGraph.php のコードは、現在 "div id="myDiv" が含まれている行にコピー アンド ペーストすると問題なく動作します)

ありがとうございました。

4

1 に答える 1

0

innerHTML の割り当てで script タグを許可すると、セキュリティ上の脅威になります。ダウンさせようとしていたデータが、フォームで提供されたユーザー提供のコンテンツであり、これが別のユーザーによって読み込まれたとします。ユーザー Bob がスクリプト タグをフォームに挿入すると、サーバー X から悪意のあるコードが読み込まれます。ユーザー Charlie が指定されたコンテンツを含むページを読み込むと、その悪意のあるコードが実行されます。これはクロスサイト スクリプティングの 1 つの形式であり、innerHTML 割り当てを使用しているときにブラウザーがこれを無効にしていても驚くことではありません。ただし、すべてのブラウザがこれを行うという保証はありません。

詳細はこちら: https://developer.mozilla.org/en-US/docs/DOM/element.innerHTML#Security_consideration

そうは言っても、あなたが望むことを行うためのより簡単な方法が2つあります。最初の方法を最善の方法としてお勧めします。

1) 更新の詳細ではなく、スクリプトの大部分を、読み込まれるが必ずしも実行されるとは限らない関数に移動します。xmlhttp 経由でスクリプト テキスト全体を取得する代わりに、データ自体を解析しやすい形式 (JSON など) で返し、そのデータを引数として更新関数の呼び出しに渡すだけです。

2) 絶対に関数呼び出しを渡す必要がある場合は、xmlhttp も必要ありません。DOM で script タグを動的に作成し、そのソースをスクリプトを生成する PHP ページに向けるだけです。script タグが追加されるとすぐに、スクリプトが読み込まれます。

于 2012-12-26T05:40:24.530 に答える