views:

45

answers:

3

I'm new to javascript, my script is designed to display either one image or the other image with a 50/50 chance. It worked in safari but not in firefox, this cuts out too many customers for the website I'm designing for.
Any help would be appreciated, especially if anyone could say how to make sure it would work across all browsers. Otherwise I can figure out how to make it ignore it in firefox or some other such combination.

var randomnumber=Math.floor(Math.random()*2)

if (randomnumber > 0)
{
    document.write("<img src="Easter.jpg">");
}
else
{
    document.write("<img src="StPatrick.jpg">");
}

Oh and the actual code has & q u o t ; instead of " where it needs to be in case you were wondering if that was making it not work. It does work in safari - just not in firefox or camino.

+2  A: 

You need to escape the inner quotes:

if (randomnumber > 0)
{
document.write("<img src=\"Easter.jpg\">");
}
else
{ 
document.write("<img src=\"StPatrick.jpg\">");
}

Inside a string which is terminated by ", when you want to insert a double-quote, you need to "escape" it by putting a \ before it

EDIT: I just saw your edit where you said the actual code has &quot; in it. If you code is:

document.write("<img src=&quot;Easter.jpg&quot;>");

That won't work. it will literally write:

<img src=&quot;Easter.jpg&quot;>

Change the &quot; to \"

Josh
and self-close the `img` element too
Russ Cam
@Russ: Only if it's XHTML or HTML 4.01 Transitional
Josh
You could also just use a single quote, ', instead of an escaped double quote.
Alex JL
true, I was thinking err on the side of caution :)
Russ Cam
@Code Duck: yes, you can. But my answer teaches him how to include quotes in a quoted string, which is necessary JavaScript knowledge.
Josh
Sure, and my comment teaches him how he can use single quotes inside a double quoted string, which is also useful JavaScript knowledge.
Alex JL
Y'all rock. The "escaping" quotes tactic made it work cross-browsers for me- thank you much Josh. I will look into the single quotes tactic as well Code Duck, thank you. God I love this site. : )
Danny
+2  A: 

Try

var randomnumber=Math.floor(Math.random()*2)

if (randomnumber > 0) {
  document.write("<img src='Easter.jpg' />");
}
else {
  document.write("<img src='StPatrick.jpg' />");
}

You might want to consider a different tactic to using document.write() though, perhaps creating the element using new Image() and appending it to the DOM.

Russ Cam
Hmm, I've never used new Image or appended the DOM but I believe I know what you're talking about, I'll look into it and see what I can find. I appreciate the advice and guidance Russ, thank you.
Danny
A: 

Your quotes need fixing. You need nested quotes in your document.wrote statements, but you're using the same quotation marks for both the outer and inner quotes.

When the browser sees the second double-quote, it closes the string it's building, so you get a string containing <img src=

HTML supports both single and doube quotes so you can alternate them to avoid this problem. Or, you an escape the inner quotes, but that's harder to read; alternating single- and double-quotes is usually all you need.

Change

document.write("<img src="Easter.jpg">")

to

document.write("<img src='Easter.jpg'>")
Val
I see...I will use this method. Thank you!
Danny