SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question File Includes with Javascript

    Hello,

    I am new to Javascript and am trying to blindly work out a way to include a file with the below script.

    Help would be greatly appeciated.

    [CODE]

    var handwidth=240
    if (screen.width>=handwidth)
    // need to include file here

    [CODE]


  2. #2
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Including a file in the client side javascript is not possible. you need to go for the server side javascript.
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the reply Vikrant but I do not understand your meaning. I wasn't aware that there was server side javascript. Please explain.

    It seems with the functionality of javascript, there must be a way to do what I need to?

  4. #4
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what kind of file you are looking to include?
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The file I'm trying to include is html/javascript but it has been saved as a .inc file.

  6. #6
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Including a file is not javascript command it is a part of the HTML
    Code:
    <script type="text/javascript" src="/js/admin/menu.js>">
    </script>
    hence you can not put a condition to include a file
    but this can be done through server side scripting. like PHP, JSP, etc.
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,840
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You can add a script tag into the page using javaScript which would have the effect of including an extra javaScript file.

    You can't use javaScript to include a file that doesn't contain JavaScript though.

    see http://javascript.about.com/library/bladdjs.htm for how to include extra JavaScript files using JavaScript.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice one......
    Thanks
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the help

  10. #10
    SitePoint Addict KJedi's Avatar
    Join Date
    Sep 2005
    Location
    Ukraine, Nikolaev
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Including is very simple.
    1. Create HTML-element:
    Code JavaScript:
    var inc = document.creatElement('script');
    2. Set src value:
    Code JavaScript:
    inc.src='script.js';
    3. Set any other values (type, language etc)
    4. Append this element to the document.
    Should work

    I've saw an article about this, can't find it now... Try to goolge for "javascript includes"

    By the way, in the same way you can load some XML structures appendng them to some HTML-elements.

    There are may interesting things to do and all of them are quite easy.

    The only nightmareof JS-programmer is IE

  11. #11
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's important to note, that including external files this way happens asynchronously. Thus, you can't write code right after the inclusion, and expect the included content to be available.


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
  •