12

I have a question. Firstly, I am not going to pretend that I know what I am talking about here. I am a newbie to http and JavaScript.

I think my question may be answered in this post IMG SRC tags and JavaScript but I thought I would explain the exact thing I am trying to achieve in case there is an easier way.

I have a webpage, I want to display an image on it. Only thing is, the image is coming from an automated system monitor, the image is automatically generated each day and placed in a new directory depending on date.

e.g. On April 4 = "http://host/partition/2009/apr/04/cpu.gif"
e.g. On April 5 = "http://host/partition/2009/apr/05/cpu.gif"

To facilitate this, I have created some basic JavaScript to give me the date in the format I need it. I have all that working. Now I just want to use that variable I created to show the image. I have the JavaScript code stored in a function called displaydate() If I do this <script language="JavaScript"> displaydate() </script> I see "http://host/partition/2009/apr/05/cpu.gif" and that is correct.

Now how do I display this on the site correctly?

 <a href="displaydate()"><img src="displaydate()" </a></td>    //This does not work. I am just adding it to show where I have been heading.

P.S. I have read a lot of pages on this and been trying a lot of things, but have had no luck so far. Any help, would be much appreciated.

4

2 回答 2

14

是的,该页面可能确实回答了您的问题。基本上,你想要这个 javascript:

<script type="text/javascript">
document.getElementById('image').src = "yourpicture.png";
</script>

除了你想用你写的函数替换“yourpicture.png”来生成磁盘上图像的正确路径,所以......

<script type="text/javascript">
document.getElementById('image').src = displaydate();
</script>

当然,您可能需要根据自己的用途对其进行一些修改,无论 < img > 标签的 id 属性是什么,getElementById 都会作为参数。您可能希望在页面加载后执行上述 javascript,即:

<html>
<head>
<script type="text/javascript">
function load()
{
document.getElementById('image').src = displaydate();
}

function displaydate()
{
//your displaydate() function here
}
</script>
</head>

<body onload="load()">

<img src="nothing.jpg" id="image" name="image"/>
</body>
</html> 
于 2009-04-20T06:47:06.150 回答
1

你应该只需要改变这一行

document.write("http://host1/Shared/" + year + "/" + month + "/" + day + "/cpu_abs.gif");

return "http://host1/Shared/" + year + "/" + month + "/" + day + "/cpu_abs.gif";
于 2009-04-24T12:42:28.217 回答