Forum Moderators: open

Message Too Old, No Replies

javascript error

i m using a javascript learnt from here only..but its not working

         

vinayakeditor

11:03 am on Dec 3, 2016 (gmt 0)

5+ Year Member



i m using a javascript learnt from here only..it was to change content daily but its not working
the code is as follows..please correct the mistake so i may use it.....i have a few pictures i want to rotate daily..ie, new picture every day.

<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
window.onload = function(){chgDailyImg();}
function chgDailyImg()
{
var imagearray = new Array('quote1.jpg','quote2.jpg','quote3.jpg','quote4.jpg');
var d = new Date();
var t = d.getTime();
var days = Math.floor(t/(86400000));
var i = days % imagearray.length;
Document.getElementById("dailyImg").src = imagearray[i];
}
</script>
</head>
<body>
<h1> <b> HI THERE GUYS! THIS PIC WILL CHANGE EVERYDAY. </b> </h1>
<img src=imagearray[i] title="daily quote" width="600" height="400" id="dailyImg" />
</body>
</html>

lucy24

9:08 pm on Dec 3, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh dear, the ever-helpful “not working”.
-- image doesn't display at all?
-- function is not invoked on load?
-- function is invoked, but does not change image at the desired interval?

Incidentally, the locution <img blahblah /> with final slash is only needed in XHTML. HTML (the page has an HTML 5 DTD) doesn't require a slash on self-closing elements.

Still more incidentally, "everyday" and "every day" have different meanings, as in the restaurant whose slogan used to be “everyday low prices ... every day”. Nobody will object if you get it right; some visitors will notice if you get it wrong.

vinayakeditor

5:51 pm on Dec 6, 2016 (gmt 0)

5+ Year Member



A border-line in place of picture comes (with its title inside) but not the picture.

not2easy

5:55 pm on Dec 6, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Are these images in the same directory as the page, or in an image directory? It looks as if the images are not accessible at the place the script is looking for them.

robzilla

6:08 pm on Dec 6, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



"Document" should be "document". Javascript is case-sensitive.

That fixes the script. Tip: use the Console of your browser to catch these sort of errors.

src=imagearray[i]

This doesn't do anything except instruct the browser to fetch an image with that name, which probably doesn't exist. Either use a fallback image in the src attribute or simply remove the attribute from the IMG tag.

Also, an H1 header is bold by default, so adding <b></b> has no effect.

lucy24

6:52 pm on Dec 6, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To put it a different way: the line
img src=imagearray[i]
(in the way that you're using it, not as literal text) only has meaning inside a <script> section. In some situations you could use innerHTML with the element's id, but it's obviously not possible in a self-closing element such as <img>. In any case you need a fallback for people with scripting turned off.

Also, an H1 header is bold by default, so adding <b></b> has no effect.
Besides, you should be styling your own headers unless you're striving for that 1997 Retro Look. Bold or not-bold, to taste, but it certainly doesn't need to be inside the <h1> tags -- unless this specific page is for some reason meant to look different from all the other <h1> on the rest of the site.

vinayakeditor

10:47 am on Dec 7, 2016 (gmt 0)

5+ Year Member



1) Both the htm document and images are in the same directory.

2) "Document..." & "document...." dont change a thing. I tried both.

3) Could you tell me (anyone) a whole new code for the same (changing pictures daily) which is tested to work. If it also doesnt work with my pc...that means that something is wrong locally. ..or just test the code i mentioned in the starting..if its working or not.

vinayakeditor

10:49 am on Dec 7, 2016 (gmt 0)

5+ Year Member



bcoz i suspect that something is wrong locally...if its with the code then plz correct.

birdbrain

12:01 pm on Dec 7, 2016 (gmt 0)



Hi there vinayakeditor,
here is your code, corrected and validated...

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>untitled document</title>
<style media="screen">
#dailyImg {
display: block;
width: 100%;
max-width: 37.5em;
height: auto;
}
</style>
</head>
<body>
<h1>HI THERE GUYS! THIS PIC WILL CHANGE EVERYDAY.</h1>
<img id="dailyImg" src="quote1.jpg" width="600" height="400" alt="daily quote">
<script>
var imagearray = ['quote1.jpg','quote2.jpg','quote3.jpg','quote4.jpg'];
var d = new Date();
var t = d.getTime();
var days = Math.floor(t/(86400000));
var i = days % imagearray.length;
document.getElementById('dailyImg').src = imagearray[i];
</script>
</body>
</html>



birdbrain

robzilla

10:05 pm on Dec 7, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



2) "Document..." & "document...." dont change a thing. I tried both.

No point in trying both: lowercase "document" is the only valid identifier. And it does change a thing: it fixes the script.

See https://jsfiddle.net/L64zbxub/1/ [jsfiddle.net], change "Document" to "document", then click "Run". (I added the suthomas.com URL, which happens to host several images named quote1.jpg, quote2.jpg, etc., so that the images can actually be downloaded. I assume you have made sure the images you want to load are in the same directory as the HTML file, as not2easy pointed out.)

If it's not working locally, open up your Console (Chrome [developers.google.com], Firefox [developer.mozilla.org]) to find any errors.

vinayakeditor

10:11 am on Dec 8, 2016 (gmt 0)

5+ Year Member



thnq u all very much! its working now.