これを実現するには、少なくとも 3 つの方法があります。
純粋な HTML
Amitdのコメントで指摘されているように、「show.html」で次の<meta>
タグをドキュメントの<head>
要素に追加します。
<meta http-equiv="refresh" content="5" />
これにより、5 秒ごとにページが自動的に更新されます。content
属性の値を目的の秒数に調整します。
純粋な JavaScript:
MeNoMoreで指摘されているようdocument.location.reload()
に、呼び出すとページが更新されます。
<script type="text/javascript">
//put this somewhere in "show.html"
//using window onload event to run function
//so function runs after all content has been loaded.
//After refresh this entire script will run again.
window.onload = function () {
'use strict';
var millisecondsBeforeRefresh = 5000; //Adjust time here
window.setTimeout(function () {
//refresh the entire document
document.location.reload();
}, millisecondsBeforeRefresh);
};
</script>
また、 tpower AJAX リクエストで指摘されているように、使用することもできますが、目的の画像への URL を返す Web サービスを作成する必要があります。AJAX リクエストを行う JavaScript は次のようになります。
<script type="text/javascript">
//put this somewhere in "show.html"
//using window onload event to run function
//so function runs after all content has been loaded.
window.onload = function () {
'use strict';
var xhr,
millisecondsBeforeNewImg = 5000; // Adjust time here
if (window.XMLHttpRequest) {
// Mozilla, Safari, ...
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// IE
try {
// try the newer ActiveXObject
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
// newer failed, try the older one
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// log error to browser console
console.log(e);
}
}
}
if (!xhr) {
// log error to browser console
console.log('Giving up :( Cannot create an XMLHTTP instance');
}
xhr.onreadystatechange = function () {
var img;
// process the server response
if (xhr.readyState === 4) {
// everything is good, the response is received
if (xhr.status === 200) {
// perfect!
// assuming the responseText contains the new url to the image...
// get the img
img = document.getElementById('theImgId');
//set the new src
img.src = xhr.responseText;
} else {
// there was a problem with the request,
// for example the response may contain a 404 (Not Found)
// or 500 (Internal Server Error) response code
console.log(xhr.status);
}
} else {
// still not ready
// could do something here, but it's not necessary
// included strictly for example purposes
}
};
// Using setInterval to run every X milliseconds
window.setInterval(function () {
xhr.open('GET', 'http://www.myDomain.com/someServiceToReturnURLtoDesiredImage', true);
xhr.send(null);
}, millisecondsBeforeNewImg)
};
</script>
その他の方法:
最後に、あなたの質問に答えてtpowerの答え...$.ajax()
は、jQueryを使用して AJAX 呼び出しを行うことです。jQuery は、AJAX 呼び出しと DOM 操作をより簡単にする JavaScript ライブラリです。jQuery ライブラリを使用するには、それへの参照を要素に含める必要があります<head>
(例としてバージョン 1.4.2 を使用)。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
「jquery.min.js」をダウンロードしてローカルでホストすることもできますが、もちろん、スクリプトのロード元の URL のみが変更されます。
上記の AJAX 関数は、jQuery を使用して記述した場合、次のようになります。
<script type="text/javascript">
//put this somewhere in "show.html"
//document.ready takes the place of window.onload
$(document).ready(function () {
'use strict';
var millisecondsBeforeNewImg = 5000; // Adjust time here
window.setInterval(function () {
$.ajax({
"url": "http://www.myDomain.com/someServiceToReturnURLtoDesiredImage",
"error": function (jqXHR, textStatus, errorThrown) {
// log error to browser console
console.log(textStatus + ': ' + errorThrown);
},
"success": function (data, textStatus, jqXHR) {
//get the img and assign the new src
$('#theImgId').attr('src', data);
}
});
}, millisecondsBeforeNewImg);
});
</script>
お分かりのように、jQuery のバージョンの方がはるかにシンプルでクリーンです。ただし、プロジェクトの範囲が狭いため、jQuery の追加のオーバーヘッドを気にする必要があるかどうかはわかりません (それだけではありません)。あなたのプロジェクトの要件が jQuery の可能性を許しているかどうかもわかりません。この例を含めたのは、単に何が何であるかというあなたの質問に答えるため$.ajax()
です。
画像を更新する他の方法があることも同様に確信しています。個人的には、画像の URL が常に変化する場合は、AJAX ルートを使用します。画像の URL が静的な場合は、おそらく<meta>
更新タグを使用します。