私は AJAX のことにはかなり慣れていませんが、コールバック関数 xmlHttpReq.onreadystatechange で変更されたステータスに基づいて、ドキュメントのグローバル変数に値を設定したいと考えています。次のようなものを使用しました。
function checkFile(fileUrl) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
if(self.xmlHttpReq == null){
alert("Your browser does not support XMLHTTPReq")
}
self.xmlHttpReq.open('HEAD', fileUrl, true);
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
if (self.xmlHttpReq.status == 200) {
window.rett = 1;
//alert(window.rett);
} else if (self.xmlHttpReq.status == 404) {
window.rett = 0;
//alert(window.rett);
}
}
}
self.xmlHttpReq.send();
}
そして、私は次のcheckFile
ようにjqueryテンプレートで使用します:
<script id="resultTemplate" type="text/x-jquery-tmpl">
<li> ${checkFile(link)} <b> {{if window.rett == 1 }} ${link} {{/if}}</b> </li>
</script>
しかしwindow.rett
、Jquery テンプレートで にアクセスすると、未定義と表示されます...
グローバル値を取得したい理由は、グローバル値に基づいて異なる GUI を生成したいからです。
たぶん、これはグローバル変数を使用する良い習慣ではありませんか? どんな提案でも大歓迎です。