SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast Incubator's Avatar
    Join Date
    Apr 2002
    Location
    Good old Belgium
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .js function call

    How do I reach functions written in an .js file from the html file?
    for example:

    Code:
    myhtm.htm:
    
    <html>
    <body>
    <script language="JavaScript" src="myscript.js"></script>
    </body>
    </html>
    
    myscript.js:
    
    <!--
    function myfunc() {
       document.write('function in external js');
    }
    -->
    when using the myfunc() function in the body of the html it keeps saying the function is not defined. So how do I reach it from there?
    "Press any key to continue. Press any other key to exit."

  2. #2
    Froot r gewd SubKamran's Avatar
    Join Date
    May 2002
    Location
    North Star State
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you including the OTHER .js file too? If not, include it ABOVE that one...
    "Sometimes little is more."
    Kamran A
    Web Dev/Designer
    Keyboard not found: Please Press F1 to Continue

  3. #3
    SitePoint Enthusiast Incubator's Avatar
    Join Date
    Apr 2002
    Location
    Good old Belgium
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what other.js?
    there is only one external .js file
    "Press any key to continue. Press any other key to exit."

  4. #4
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Script tag should be in the head of the document.

    Code:
    <html>
    <head>
     <script language="JavaScript" src="myscript.js"></script>
    </head>
    <body>
    </body>
    </html>

  5. #5
    SitePoint Enthusiast Incubator's Avatar
    Join Date
    Apr 2002
    Location
    Good old Belgium
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not if the content of the .js can only be done in the body.
    Believe me, I made that mistake on my exam too, and I had my entire out^put on a white background, not the desired result
    "Press any key to continue. Press any other key to exit."

  6. #6
    SitePoint Evangelist compwizard's Avatar
    Join Date
    May 2002
    Location
    United States
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No different than if the function were inline.

    PHP Code:
    myhtm.htm:

    <
    html>
    <
    body>
    <
    script language="JavaScript" src="myscript.js"></script>

    <input type="button" onclick="myfunc();">
    </body>
    </html>

    myscript.js:

    <!--
    function myfunc() {
       alert('function in external js');
    }
    --> 
    Compwizard
    "There are 10 kinds of people in this world -- those who know binary, and those who don't."

  7. #7
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A better way (imho)...

    First, link to the external .js file as vibe35 has shown (i.e. in the header)

    To call just the function inline in the body.
    Code:
    <body>
    
    ...
    
    <script type="text/javascript">myfunc();</script>
    ...
    
    </body>
    </html>
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  8. #8
    SitePoint Evangelist compwizard's Avatar
    Join Date
    May 2002
    Location
    United States
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want the function to run immediatly during pageload, then sure, use this:
    Code:
    <body>
    
    ...
    
    <script type="text/javascript">myfunc();</script>
    ...
    
    </body>
    </html>
    However, if you want to be able to use functions in the external file to do other things like when a link, button or submit button is clicked, then use my way.

    It just depends what you need the external fucntions to do.
    Compwizard
    "There are 10 kinds of people in this world -- those who know binary, and those who don't."

  9. #9
    SitePoint Enthusiast Incubator's Avatar
    Join Date
    Apr 2002
    Location
    Good old Belgium
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks
    "Press any key to continue. Press any other key to exit."

  10. #10
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Plus, the <!-- and --> comment tags are not valid in an external JS file, only as part of an HTML document.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!


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
  •