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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How do open external .js files?

    Hi everyone,

    I'm a newbie and this is probably really easy but it just won't work!
    I've tried looking at various online sources but they are all either too technical or don't seem to work. I'm obviously doing something wrong!

    The code is posted below, it's not very long so hopefully someone will be able to debug it!

    Thanks lots!

    Original page:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <title>variables</title>
    </head>
    <body>
    <script src="displaydate.js" type="text/javascript"></script>
    <br>
    </body>
    </html>

    External displaydate.js file:

    Code:
    function todaydate(){
    var today_date= new Date()
    var myyear=today_date.getYear()
    var mymonth=today_date.getMonth()+1
    var mytoday=today_date.getDate()
    document.write(myyear+"/"+mymonth+"/"+mytoday)
    }
    Opening the original page in my browser displays nothing. Just a blank page.

    Any help you can give would be greatly appreciated!

    Geoff
    Last edited by sirbassface; 12-19-2011 at 05:27 PM.

  • #2
    New to the CF scene
    Join Date
    Dec 2011
    Location
    Southern California
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Thats because you have not called your function, try this:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <title>variables</title>
      <script src="displaydate.js" type="text/javascript"></script>
    </head>
    <body onload="todaydate()">
    
    <br>
    </body>
    </html>

  • Users who have thanked theTortoise for this post:

    sirbassface (12-19-2011)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Quote Originally Posted by theTortoise View Post
    Thats because you have not called your function, try this:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <title>variables</title>
      <script src="displaydate.js" type="text/javascript"></script>
    </head>
    <body onload="todaydate()">
    
    <br>
    </body>
    </html>
    That isn't going to work because the document.write must be run BEFORE the page finishes loading or it will overwrite te entire page.

    The simplest way is to just call the function immediately after defining it.
    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.

  • Users who have thanked felgall for this post:

    sirbassface (12-19-2011)

  • #4
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    That isn't going to work because the document.write must be run BEFORE the page finishes loading or it will overwrite te entire page.

    The simplest way is to just call the function immediately after defining it.
    Thankyou both for your help.
    Felgall could you please give me an example of what you mean? I have tried this

    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <title>variables</title>
    </head>
    <body>
    <br>
    Is this working now? Who knows?
    <script src="displaydate.js" type="text/javascript">
    document.write(myyear+"/"+mymonth+"/"+mytoday)
    </script>
    </body>
    </html>
    and I've tried adding the

    Code:
    <script src="displaydate.js" type="text/javascript">
    document.write(myyear+"/"+mymonth+"/"+mytoday)
    to the head section. Neither seem to work.
    Where exactly should I call the function immediately after defining it?

    Thanks again for the advice!

  • #5
    New Coder
    Join Date
    Dec 2011
    Location
    Earth if you want to know
    Posts
    27
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by sirbassface View Post
    Where exactly should I call the function immediately after defining it?
    Hi there,

    Please replace your:

    Code:
    function todaydate() {
        var today_date = new Date();
        var myyear = today_date.getYear();
        var mymonth = today_date.getMonth() + 1;
        var mytoday = today_date.getDate();
        document.write(myyear + "/" + mymonth + "/" + mytoday);
    };
    By this:

    Code:
    (function todaydate() {
        var today_date = new Date();
        var myyear = today_date.getYear();
        var mymonth = today_date.getMonth() + 1;
        var mytoday = today_date.getDate();
        document.write(myyear + "/" + mymonth + "/" + mytoday);
    })();
    This will make your code run immediately after it is loaded.

    Explanation:

    If you have a code:

    Code:
    function bakeThatUnicorn() {
        alert("BAKED!");
    };
    And you call it:

    Code:
    bakeThatUnicorn();
    You just simply parse the variable bakeThatUnicorn (which returns the function bakeThatUnicorn), then executed the parsed result by adding ().

    The same theory applied, instead of call it through a variable like that function name, you can immediately parse the function, and execute it.

    Code:
    (function bakeThatUnicorn() {
       //Fancy stuffs with fancy recipe
    })();
    Those parenthesis () are used to parse anything (for example, (1 + 2) will parse 1 + 2 and return 3). So in this case, it will parse the function. Then just simply execute it by adding ().

    I hope this help.

    Tim_
    Last edited by cuzMazn; 12-18-2011 at 10:25 PM.

  • Users who have thanked cuzMazn for this post:

    sirbassface (12-19-2011)


  •  

    Posting Permissions

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