Results 1 to 11 of 11

Thread: Simple IF / ELSE statement not working. Wrong syntax?

  1. #1

    Simple IF / ELSE statement not working. Wrong syntax?

    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:
    JavaScript Block
    <script type="text/javascript">
    //<![CDATA[
    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.");
    }
    /*]]*/>
    </script>


    Thanks for your help
    Last edited by diades; 24th May 2012 at 14:50.

  2. #2
    Senior Member
    England coothead's Avatar
    Join Date
    Feb 2003
    Location
    Chertsey, a small town 25 miles south west of London, England.
    Posts
    633
    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

  3. #3
    Thank you!

    Yes, it is saved as .html

    My HTML code is:
    HTML Code:
    <!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.


  4. #4
    Senior Member
    England coothead's Avatar
    Join Date
    Feb 2003
    Location
    Chertsey, a small town 25 miles south west of London, England.
    Posts
    633
    Hi there afish88,

    check out the attachment to see my test page.

    coothead
    Attached Files Attached Files

  5. #5
    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!

  6. #6
    Senior Member
    England coothead's Avatar
    Join Date
    Feb 2003
    Location
    Chertsey, a small town 25 miles south west of London, England.
    Posts
    633
    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.

    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

  7. #7
    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!

  8. #8
    Senior Member
    England coothead's Avatar
    Join Date
    Feb 2003
    Location
    Chertsey, a small town 25 miles south west of London, England.
    Posts
    633
    Hi there afish88,

    it may be helpful for you to visit the "Adobe Dreamweaver Forum" here...

    ...and see what suggestions are offered for you problem.

    coothead

  9. #9
    I think I might give that a go.

    Thank you for your help!

  10. #10
    Senior Member
    England coothead's Avatar
    Join Date
    Feb 2003
    Location
    Chertsey, a small town 25 miles south west of London, England.
    Posts
    633
    No problem, you're very welcome.
    coothead

  11. #11
    Moderator
    United Nations Vincent Puglia's Avatar
    Join Date
    Jan 2000
    Location
    where the World once stood
    Posts
    4,961
    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
    Last edited by Vincent Puglia; 2nd July 2012 at 13:47.
    The Blades of Grass cut me still
    grassBlade

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. submit throws error (wrong syntax?)
    By AngelaFrench in forum Client-side forum
    Replies: 6
    Last Post: 16th May 2008, 16:53
  2. PHP syntax on SET statement
    By AngelaFrench in forum Server-side Forum
    Replies: 0
    Last Post: 1st August 2006, 18:32
  3. update statement syntax error
    By zenny in forum Server-side Forum
    Replies: 3
    Last Post: 12th April 2005, 10:58
  4. help with syntax error in insert statement
    By AngelaFrench in forum Server-side Forum
    Replies: 2
    Last Post: 27th April 2004, 17:26
  5. very simple syntax question
    By AngelaFrench in forum Server-side Forum
    Replies: 3
    Last Post: 7th May 2002, 22:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •