0

I'm a JavaScript newbie, just doing a little script so that I can rotate an image + link every few days. I'm 90% sure my error is in the bottom line of code - seems to be something to do with the way quotes are used (which is confusing me a lot).

Correct code + explanation of where and why to use quotes would be much appreciated!

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
today = new Date();
date = today.getDate();
date = date - date%9;
date = date/9;
date = date%3;

arday = new Array("image 1", "image 2", "image 3");

linkday = new Array("link 1", "link 2", "link 3");

document.write("<a href= '" + linkday[date] + target="_blank" ><img src='" + arday[date] "'/> </a>");
// End -->
</SCRIPT>
Jordan Smith
  • 10,310
  • 7
  • 68
  • 114
  • 1
    What *is* wrong with it? Look at the syntax highlighting. –  May 25 '12 at 00:10
  • @PenchoIlchev It should actually not be. This is a legacy feature (that should actually *be removed*). `// –  May 25 '12 at 00:11
  • @PenchoIlchev Sure, but useless comments are fun ;-) –  May 25 '12 at 00:13
  • @everyone - hah, looks like my code is pretty crude then - shows how unhelpful copy/pasting code from forums and website tutorials can be! – Jordan Smith May 25 '12 at 00:24

3 Answers3

3

You need to escape some quotes and add some other ones in your document.write statement:

document.write("<a href='" + linkday[date] + "' target='_blank'><img src='" + arday[date] "' /></a>");

When you use variables, you should also use var to prevent them from automatically attaching to the global namespace. In this case, there is no visible difference, but there would be a difference if the above code was in a function. So the following variable assignments would be preferred:

var today = new Date();
var date = today.getDate();
date = date - date % 9;
date /= 9;
date %= 3;

var arday = ["image 1", "image 2", "image 3"];
var linkday = ["link 1", "link 2", "link 3"];

By the way, the proper tag to use is

<script type="text/javascript">

language has been deprecated. The type attribute can be omitted in HTML5, however, if that is the doctype that you are using.

Also, the comments

<!-- Begin
...
// End -->

can be removed, as they don't serve much of a purpose in modern-day browsers, and actually does comment out the script if a browser is fully following XHTML.

Community
  • 1
  • 1
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
2

Go from the correct string and add things in

<a href="" target="_blank" ><img src=""/></a>

Wrap it in single quotes since the string above is using double quotes.

'<a href="" target="_blank" ><img src=""/></a>'

Add the first variable reference

'<a href="' + linkday[date] + '" target="_blank" ><img src=""/></a>'

Add the second variable

'<a href="' + linkday[date] + '" target="_blank" ><img src="' + arday[date] + '"/></a>'

wrap it in the document.write

document.write('<a href="' + linkday[date] + '" target="_blank" ><img src="' + arday[date] + '"/></a>');
epascarello
  • 204,599
  • 20
  • 195
  • 236
2
  • The language attribute is ignored by browsers, remove it. Even the type attribute can be omitted.
  • If you are not parsing this page as XML, remove the <!-- -->
  • Use array literals instead of new Array()
  • Use var when declaring variables
  • Chain variable declarations using commas. Notice one var and a series of variable declarations.
  • Although single quotes are accepted, in HTML, use double quotes "" for attribute values.
  • To avoid escaping double quotes, wrap strings in single quotes.
  • There shall be no spaces between the opening bracket < and the tag name.
  • There shall be no spaces between the attribute name, the equal sign and the opening quote.

Here's a cleaned code:

<script>
    var today = new Date(),
        date = today.getDate(),
        arday = ["image 1", "image 2", "image 3"],
        linkday = ["link 1", "link 2", "link 3"];

    date = date - date % 9;
    date = date / 9;
    date = date % 3;

    document.write('<a href="' + linkday[date] + '" target="_blank" ><img src="' + arday[date] + '" / ></a>');​
</script>
Joseph
  • 117,725
  • 30
  • 181
  • 234