如何使用JS从另一个页面检索图像路径

How can I retrieve an image path from another page using JS?

本文关键字:面检索 图像 路径 另一个 何使用 JS      更新时间:2023-09-26

这是我的情况:我有一个页面的横幅会经常更新,这意味着每次更新页面时横幅的图像路径都会不同(无法覆盖)。无论如何,它将位于div或具有常量名称的容器中。

我需要做的是检索该图像路径并将其打印到另一个页面中,因此,如果横幅在第一个页面中发生更改,它将在第二个页面中自动发生更改。

我想也许一些javascript可以完成这项工作,但我真的不确定如何从div内部获取图像路径。

如有任何帮助,我们将不胜感激,来自阿根廷的问候

使用html5和javascript的解决方案就是这个

  you can get the image tag through javascript(as u say it is in div
  and whose id you know)  
  something like
  src = document.getElementById("id").childnodes[0].src
  should work for u
  then you can store this src in the localStorage
  localStorage["src"] = src;
  as soon as you store something in localstorage it will fire a 
  "storage" event in all the other pages except the page in which 
  you have actually stored the src

 so handle "storage" event in the other pages like this
     window.addEventListener("storage",func,false);
     function func(event)
     {
         src  = localStorage[event.key];
        //now src has the updated src :)
     }