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 14 of 14
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Using SRC="Javascript program"

    Why doesn't this work? Both files are in the same folder. I'm sure I've done this many times before???

    HTML File "Test.htm" :-
    Code:
    <html>
    <head>
    <script type="text/javascript" src="Test.js"></script>
    </head>
    <body>
    <h1>My First Web Page</h1>
    <p id="demo">This is a paragraph.</p>
    <button type="button" onclick="displayDate()">Display Date</button>
    </body>
    </html>
    javascript File "Test.js" :-
    Code:
    function displayDate(){
    document.getElementById("demo").innerHTML=Date();
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,156
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    document.getElementById("demo").innerHTML= new Date();


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    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.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks Philip, that was not the problem.

    The problem is somewhat obscure. There are no apparent errors in the programs. If you copy past the program in your own computer it will not run.

    The problem is that there are some invisible UTF-16 characters embedded in the program that I can't get to show on my editor but you can see evidence if you look at the browser Error Console when you run it.

    Copy/paste to Notepad and re-copy/paste to the htm file and now it will work.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,156
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by Ann-Marie View Post
    Thanks Philip, that was not the problem.

    There are no apparent errors in the programs. If you copy past the program in your own computer it will not run.
    It works for me when I correct the error in your program which is indeed quite apparant.

    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
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    OK the program I was using for testing is a copy paste out of the w3schools.com tutorials modified to use the SRC= function.

    I introduced a problem by selecting all of an old file and pasting the test file over it. Selecting all the old file DID NOT select the invisible UTF-16 characters so they got left behind.

    The original works fine in their tutorial and after removing the UTF-16 characters it works for me without your suggestion.

    My editor is having serious problems with UTF-16 characters so I think I will remove ALL the UTF-16 text from the htm file and save them separately in a script file dedicated to the text look-up.

    I appreciate your suggestion and I will see if it should be included for "correct" scripting.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,156
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    It is always amusing when youngsters attempting their "first web page" try to teach their grandmothers to suck eggs.

    You need to note that JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.

    Code:
    <script type="text/javascript">
    
    var d= new Date();
    alert (typeof d);  // object
    document.write(d);
    var d = Date();
    alert (typeof d);  // string
    document.write (d);
    
    </script>
    So while Date() works to display the current date in this simple example it does not return a Date object which can be manipluated. OK?

    Code:
    <script type = "text/javascript">
    
    var myDate=new Date();
    myDate.setDate(myDate.getDate()+5);
    alert (myDate);  // current date plus 5 days
    
    var myDate=Date();
    myDate.setDate(myDate.getDate()+5);
    alert (myDate);  // error
    }
    </script>
    I would be interested to se a link to the w3schools tutorial script you say you have copied - I do not find it.
    Last edited by Philip M; 12-25-2011 at 10:20 AM. Reason: Typo

    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.

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    LOL thanks for the compliment Philip. I've been a computer programmer since before you were born.

    It was modified from
    http://w3schools.com/js/tryit.asp?filename=tryjs_events

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,156
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by Ann-Marie View Post
    LOL thanks for the compliment Philip. I've been a computer programmer since before you were born.

    It was modified from
    http://w3schools.com/js/tryit.asp?filename=tryjs_events
    In that case you must be very, very old!!! Were there any computer programmers before I was born????


    Bessie Braddock to Winston Churchill - "Sir, you are disgustingly drunk".
    Winston Churchill - "Madam, you are disgustingly ugly. But I will be sober in the morning".
    Last edited by Philip M; 12-26-2011 at 03:20 PM.

    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.

  • #9
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Oh yes, I started on UNIVAC III in 1959.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,156
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by Ann-Marie View Post
    Oh yes, I started on UNIVAC III in 1959.

    That was not befoe I was born! But perhaps it was before you were born.

    Wikipedia informs me Sperry Rand began shipment in June 1962 and produced 96 UNIVAC III systems so you were very precocious to be working on one in 1959. If you were 19 then you must be, er, 71 now.
    Last edited by Philip M; 12-26-2011 at 04:07 PM.

    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.

  • #11
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    No, I was born in 1939. UNIVAC III had just been installed in the University of NSW in Australia. It was my last year at the University and I was in their very first assembly language course. I swear by the end of the year I knew more about programming it than the lecturer. Those were the days of 80 column input/output cards, 3 hours each morning finding and replacing vacuum tubes that had died over night, 8 bit ultrasonic mercury delay lines for RAM. Yes I'm a novice in javascript but I've been an assembly language programmer for over 50 years.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,156
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by Ann-Marie View Post
    No, I was born in 1939. UNIVAC III had just been installed in the University of NSW in Australia. It was my last year at the University and I was in their very first assembly language course. I swear by the end of the year I knew more about programming it than the lecturer. Those were the days of 80 column input/output cards, 3 hours each morning finding and replacing vacuum tubes that had died over night, 8 bit ultrasonic mercury delay lines for RAM. Yes I'm a novice in javascript but I've been an assembly language programmer for over 50 years.
    I take my hat off to you! I did not start programming (in ALGOL) until 1965.

    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.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Boy, you both make me feel like a real novice!

    The first time I wrote a computer program, I did my roommate's Fortran II homework for him in 1964, but that was only one program so I don't think it counts. I didn't really start programming for real until 1969 when I got access to a time sharing computer that had BASIC, Fortran, and PL/I on it. My first "assembly language" was on a programmable calculator (don't even remember who made it) that used punch cards, but you had to hand-punch the cards (they were pre-perforated chads) with a little metal stylus. Each instuction took one column on the card and was given as a pair of hexadecimal numbers (so there were 8 "chads" per column plus 1 you could punch to indicated to skip the column because you had goofed).

    The first computer I really had "hands on" was an IBM 360/75 in 1971.

    However, I will lay claim to having written the first BASIC interpreter for a microprocessor, which I did in 1972/73. (A 4-bit Rockwell PPS-4, if it matters.) I haven't met anybody yet who can claim an earlier date.
    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.

  • #14
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,401
    Thanks
    11
    Thanked 595 Times in 575 Posts
    i started learning in 2007.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Tags for this Thread

    Posting Permissions

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