daverichards
daverichards

Reputation: 5

Reloading image on page reload

I am trying to make an image reload each time I reload the page and I am trying this code:

d = new Date();
$("#myimg").attr("src", "/myimg.jpg?"+d.getTime());

Can someone help me add this code to a page???

I have this very stubborn piece of code:

<head>
  <script>
    d = new Date();
    ("#myimg").attr("src", "http://i66.tinypic.com/6fme5c.jp?d=" + d.getTime());
  </script>
</head>
<body>
  <img src="http://i66.tinypic.com/6fme5c.jpg" class="resize">
  <img src="myimg" class="resize" id="myimg">
</body>

Chrome does nto understand what myimg is(:

Upvotes: 0

Views: 80

Answers (1)

Lewis Browne
Lewis Browne

Reputation: 932

There are a number of issues as to why your code is not working;

Firstly, you will need to add jQuery(document).ready() This is because the image element may not exist at the time of the javascript code being executed.

Next, you are missing jQuery or $ before ("#myimg").

Finally, make sure that you are including the jQuery js file before you are running your internal js.

See code updated below:

<script>
    jQuery(document).ready(function($){
        d = new Date();
        $("#myimg").attr("src","http://i66.tinypic.com/6fme5c.jp?d=" + d.getTime());
    )};
</script>    

Upvotes: 1

Related Questions