PDA

For continued disscussion on this topic : JavaScript Simple IF / ELSE statement not working. Wrong syntax?



afish88
16th January 2012, 15:08
Hi, I'm new to JavaScript. I'm currently working through a book to learn the language.

I've just written a simple IF / ELSE statement but it doesn't do anything in my browser.

Dreamweaver says the syntax is correct, so I can't work out why it won't work.

Code:
var thesport="Golf";
var myfood="Pizza";

if (thesport=="Football") {
window.alert("Cool sport!");
}
else {
window.alert("That sport might be cool.");
}

if (myfood=="Pizza") {
window.alert("My favourite food!");
}
else {
window.alert("That food sounds ok I guess.");
}

Thanks for your help :)

coothead
16th January 2012, 16:21
Hi there afish88.

and a warm welcome to these forums. ;)

I tested you code and it worked OK.

How are you saving the file?
Does it have an .html extension?

coothead

afish88
16th January 2012, 16:35
Thank you!

Yes, it is saved as .html

My HTML code is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<script type="javscript/text" src="prjs6_1.js"></script>
</body>
</html>

The JavaScript file is named the same as it is in the script tags and is in the same directory (Dreamweaver picks it up as an associated file.)

When I preview the page in Firefox I am just presented with a blank page.

:(

coothead
16th January 2012, 16:45
Hi there afish88,

check out the attachment to see my test page. ;)

coothead

afish88
16th January 2012, 17:15
Hey thanks,

I just tried your code and it worked.

So I thought okay, what's different? I gradually changed the code you sent, testing after each bit that it still worked. (For example I changed your alert prompt back to my window.alert prompt)

After a while the code in both files was exactly the same (albeit the one you sent me was in a folder on my desktop). It still worked fine!

So I thought okay, I'll rename it and copy it into the folder that has my original script in, then change the link in my HTML to the renamed file. It then stopped working again. Still works from my desktop.

Finally, I re-downloaded your code again, didn't edit it, renamed it and linked it from my original HTML document. It stopped working.

Its gotta be something to do with previewing it from Dreamweaver. Although I've tested other scripts and they all work. I'm so confused!

coothead
16th January 2012, 17:31
Hi there afish88,

I do not use, and know nothing about "Dreamweaver", other than it is not a browser.

Personally, I use "Notepad" and two pinkies for my coding excursions. :D

Testing of code should always be done in a "Standards Compliant" browser such as Firefox, Opera, Safari or IE9.

How it looks or behaves in "Dreamweaver" is irrelevant. ;)

coothead

afish88
16th January 2012, 17:44
Sorry, I wasn't very clear before when I said "from Dreamweaver".

Dreamweaver allows you to preview in a browser. My preference being Firefox. Basically you press F12 and it opens your code in whatever browser you set up. But Firefox only displayed a blank page with no prompts. Whenever I've used this method before I've had no problems.

As I say, it works if you just double click on the HTML file. Which is odd.

I've learnt not to ever trust Dreamweaver's preview as it tends to be"optimistic" at best and downright wrong at worst.

I like to hand code in Dreamweaver because it checks the syntax, which is good for me as I'm quite new to web design and it lets me see when I've done a typo!

coothead
16th January 2012, 17:51
Hi there afish88,

it may be helpful for you to visit the "Adobe Dreamweaver Forum" here...
http://forums.adobe.com/community/dreamweaver
...and see what suggestions are offered for you problem. ;)

coothead

afish88
16th January 2012, 19:23
I think I might give that a go.

Thank you for your help!

coothead
16th January 2012, 19:33
No problem, you're very welcome. ;)
coothead

Vincent Puglia
2nd July 2012, 13:42
Hi afish88,

I also do not use Dreamweaver (is that the program that precedes all of its variables with "mm_", as in mickey mouse?)

However, if the above code is the first time you have used the include statement (script type="javscript/text" src="prjs6_1.js"), I suspect the problem may be in one of two places.

1) Either Dreamweaver's settings, specifically the directory search settings -- if it has any. Or,

2) your spelling of the phrase "...type="javscript/text"....

And, if the above is the actual spelling in your code, don't rely on Dreamweaver's spell checking.

you might also want to go whole hog and change it to:

type="text/javascript"

:)

Vinny