在Javascript中确定图像的创建日期?

时间:2010-10-11 18:33:00

标签: javascript ajax webcam

我正在尝试创建一个显示和刷新网络摄像头图像的页面( camimg.jpg ),但会显示静态图像( notlive.jpg )如果图像最近没有更新。我找到了AJAXCam脚本,它用于以设定的间隔( 15秒)重新加载“实时”图像,但没有Javascript经验我无法弄清楚如何确定图像上次更新的时间,以决定是否应显示camimg.jpg或notlive.jpg。

我对编程的经验有限,我认为它应该是:

pageLoaded = time page loaded in browser
imageUpdated = time the image was uploaded to server

if imageUpdated is not within 20 seconds of pageLoaded:
    display notlive.jpg
else:
    run AJAXCam

AJAXCam代码(最初由<body onLoad="holdUp()">调用)如下:

<script type="text/javascript">
<!--
//
//AJAXCam v0.8b (c) 2005 Douglas Turecek http://www.ajaxcam.com
//
function holdUp()
{
//
// This function is first called either by an onLoad event in the <body> tag
// or some other event, like onClick.
//
// Set the value of refreshFreq to how often (in seconds)
// you want the picture to refresh
//
refreshFreq=15;
//
//after the refresh time elapses, go and update the picture
//
setTimeout("freshPic()", refreshFreq*1000);
}

function freshPic()
{
//
// Get the source value for the picture
// e.g. http://www.mysite.com/doug.jpg and
//
var currentPath=document.campic.src;
//
// Declare a new array to put the trimmed part of the source
// value in
//
var trimmedPath=new Array();
//
// Take everything before a question mark in the source value
// and put it in the array we created e.g. doug.jpg?0.32234 becomes
// doug.jpg (with the 0.32234 going into the second array spot
//
trimmedPath=currentPath.split("?");
//
// Take the source value and tack a qustion mark followed by a random number
// This makes the browser treat it as a new image and not use the cached copy
//
document.campic.src = trimmedPath[0] + "?" + Math.random();
//
// Go back and wait again.
holdUp();
}

// -->
</script>

我正在努力实现的目标是什么?如果是这样,我将如何实施呢?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

无法在javascript中执行此操作。

唯一的选择是,如果您可以访问PHP等服务器端语言,您可以发送一个简单的HEAD请求并检查映像的Last Modified标头,然后通过Ajax将其报告给浏览器。