Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript code problems.

    Hello,

    I don't know what's wrong on this java code.


    <html>
    <head>
    <style type="text/javascript">
    selection = round(getSeconds() / 2);
    switch(selection) {

    case 1: document.writeln("Did you know I'm to lazy to write these tips?");

    case 2: document.writeln("Did you know there are 57 other tips I didn't bother writing?");
    }
    </style>
    </head>
    </html>

    It's mentioned to be a 'Did you know that' box.

    Could someone explain why I just get a white screen with this code? :P

    Thanks to read,

    Niely,

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    I don't know what's wrong on this java code.
    It's javascript not java. That's a cup of coffee and another language.
    getSeconds() of what?
    The getSeconds() method returns the seconds (from 0 to 59) of the specified date and time.
    http://www.w3schools.com/jsref/jsref_getseconds.asp OR
    https://developer.mozilla.org/en-US/...ate/getSeconds
    Evolution - The non-random survival of random variants.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,457
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Also document.write is as dead as Netscape 4. If you are going to learn JavaScript there's no point using commands that went out of use back in the 20th Century.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Not often you see so many errors in so few lines.

    this java code

    <style type="text/javascript">

    selection = round(getSeconds() / 2);

    </style>

    I'm to lazy


    Quizmaster: Which French miliatary leader was born in Corsica in 1769?
    Contestant: Charles de Gaulle
    Last edited by Philip M; 08-24-2013 at 07:56 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Aug 2013
    Posts
    18
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Yes right, getSecond() returns the seconds of local time but between o to 59. ANd you also mentioned getSecond(), but of what can you please tell me, Check here for the getSecont(). https://developer.mozilla.org/en-US/...ate/getSeconds
    And one more thing, it is JavaScript, not JAVA as you mentioned in the question.

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Just because it's so much fun, I want to make a list:

    1. Javascript runs in script attributes, not style attributes.
    2. A missing "var" keyword will create a global variable (in this context it will be anyway, it's still bad practice).
    3. round is undefined in global context; you're looking for Math.round.
    4. getSeconds() is undefined in global context; you're looking for new Date().getSeconds().
    5. document.writeln is so old. Really old. Really, really old. Don't use it.
    6. Your case statements are missing a break statement (unless you intended to do a fallthrough).

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And the word is "too", not "to", as Philip pointed out:
    "Did you know I'm to lazy to write these tips?"
    And he is outputting body content using document.write in the <head>...</head> section of the page, where AT THE LEAST it should be between <body>...</body>.

    And he is missing the <body> and </body> tags.

    And he is missing a <!DOCTYPE>.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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