Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否设置图像';src属性中的url使图像在internet中更改时可以更改_Javascript_Html_Image_Src_Windows Desktop Gadgets - Fatal编程技术网

Javascript 是否设置图像';src属性中的url使图像在internet中更改时可以更改

Javascript 是否设置图像';src属性中的url使图像在internet中更改时可以更改,javascript,html,image,src,windows-desktop-gadgets,Javascript,Html,Image,Src,Windows Desktop Gadgets,形成我的问题标题真的很难。。。我正在写我自己的windows边栏小工具,我想显示当前的月球阶段,所以我在互联网上搜索,我得到了一个网站,显示: http://www.iceinspace.com.au/moonphase.html 如果我使用图像url(http://www.iceinspace.com.au/moon/images/phase_150_095.jpg)并在imagesrc属性中设置: <img id="CurrentMoon" src="http://www.icein

形成我的问题标题真的很难。。。我正在写我自己的windows边栏小工具,我想显示当前的月球阶段,所以我在互联网上搜索,我得到了一个网站,显示:

http://www.iceinspace.com.au/moonphase.html
如果我使用图像url(
http://www.iceinspace.com.au/moon/images/phase_150_095.jpg
)并在image
src
属性中设置:

<img id="CurrentMoon" src="http://www.iceinspace.com.au/moon/images/phase_150_095.jpg"></img>

如果在internet上更改图片,图片是否会更改???

如果服务器(internet)上的图像更改,但名称相同且未缓存,则当您通过JavaScript加载图像时,图像也会更改

但是,如果图像被缓存,那么它将不会更改(您将看到旧图像)

要查看服务器上显示的最新图像,每次发出请求时都需要在URL中附加一个唯一的字符串。 比如说,

function showFlyout(event)
{
    var d = new Date();
    var n = d.getTime();
    document.getElementById("CurrentMoon").src = "http://www.iceinspace.com.au/moon/images/phase_150_095.jpg?v="+n;
}

您所说的是正确的,如果/moon/images/phase_150_095.jpg处的图像被一个新图像替换,即图像文件被更改,但图像的URL保持不变,您的小部件将正常工作,但碰巧它们通过更改图像URL来更改图像(例如,现在是phase_150_099.jpg),因此,如果将img的src属性设置为固定URL,它将显示相同的图像。正确的解决方案是:

1) 假设您的小工具来源与www.iceinspace.com不同,请使用CORS或JSONP向iceinspace发出跨来源请求 2) 创建一个document对象并通过XPath查找获取image元素,如下所示

function showFlyout(event)
{
  url = http://www.iceinspace.com.au/moonphase.html;
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.open('GET', url, true);
  xmlhttp.send(null);
  moonphasedoc = document.implementation.createHTMLDocument("");
  moonphasedoc.open("replace");
  moonphasedoc.write(xmlhttp.responseText);
  moonphasedoc.close(); 
  var element = moonphasedoc.evaluate( '//body/table/tbody/tr/td[3]/p/table/tbody/tr/td/table[1]/tbody/tr/td[1]/img' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue; //just copied the Xpath from element inspector :D
  document.getElementById("CurrentMoon").src = element.src;
 }

注意:只有在您具有访问控制允许来源的情况下,这才有效:*在iceinspace上

是的,即使缓存了映像,您的请求也会从服务器获取最新映像。我认为这是正确的方法,但不起作用:(,我如何知道我是否有“Access Control Allow Origin on the iceinspace?@KaramNajjar您可以在来自iceinspace服务器的响应标题中检查它(您可以在chrome/firefox的inspect元素中看到这些标题),但我确信iceinspace不会打开Access Control Allow Origin to all,在这种情况下,您可以要求站点管理员为您设置。
function showFlyout(event)
{
  url = http://www.iceinspace.com.au/moonphase.html;
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.open('GET', url, true);
  xmlhttp.send(null);
  moonphasedoc = document.implementation.createHTMLDocument("");
  moonphasedoc.open("replace");
  moonphasedoc.write(xmlhttp.responseText);
  moonphasedoc.close(); 
  var element = moonphasedoc.evaluate( '//body/table/tbody/tr/td[3]/p/table/tbody/tr/td/table[1]/tbody/tr/td[1]/img' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue; //just copied the Xpath from element inspector :D
  document.getElementById("CurrentMoon").src = element.src;
 }