SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict dr_dumb99's Avatar
    Join Date
    Sep 2004
    Location
    Pakistan
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How To Include one javascript file in other Javascript file

    Hi!
    How to include one javascript file in other javascript file , like in PHP we use following code to include other files.

    PHP Code:
    include_once "filename.php"
    Thanks in advance

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You don't have to. If you just put them both in the HTML they will have access to each other:
    HTML Code:
    <script type="text/javascript" src="file1.js"></script>
    <script type="text/javascript" src="file2.js"></script>
    So for example if you call the function "doSomething" in file2.js and doSomething is declared in file1.js, it will work. The only thing is that you have to be careful what order you put the <script> tags in.

  3. #3
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also dynamically create a reference to a script file inside another script file by creating a SCRIPT node, setting its type and src, and appending it to the document HEAD.

  4. #4
    SitePoint Addict dr_dumb99's Avatar
    Join Date
    Sep 2004
    Location
    Pakistan
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I include javascript as follows

    <script type="text/javascript" src="file1.js"></script>

    Will it increase the size of html document?

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, it will decrease the size of the HTML document, compared to putting the JS directly into the <script> tags. But the browser still has to download "file1.js". The advantage is that the browser can then cache this file, instead of downloading it every time like it would if you had embedded javascript in the <head>.


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
  •