SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I "include()" JavaScript files dynamically? (ala PHP)

    Hi,

    In PHP you can do this:
    PHP Code:
    $file './foo.php';
    include(
    $file); 
    Is it possible to do the same thing in JavaScript, without reloading the page, i.e:
    Code:
    var file = window.prompt('Please enter a file name:', '');
    include(file);
    Obviously include() isn't going to work, but is there some other function (or maybe it requires a lot of custom code) that will allow this?

    Thanks,
    Chen

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean, into the HTML, or pull this in from a separate script file itself?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Why not just include your JS like so:
    HTML Code:
    <script type="text/javascript" src="/myJSfile.js"></script>
    Unless I'm misunderstanding something, this should be what you need.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, since Vinnie laid out the HTML method, I'll show you the script method
    HTML Code:
    [b][color=black]function[/color][/b] addScript[b][color=navy]([/color][/b] src [b][color=#000000])[/color][/b]
    [b][color=navy]{[/color][/b]
      [color=navy][b]var[/b][/color] s [color=blue]=[/color] [color=purple]document[/color].createElement[b][color=navy]([/color][/b] [color=red]'script'[/color] [b][color=navy])[/color][/b];
      s.setAttribute[b][color=navy]([/color][/b] [color=red]'type'[/color], [color=red]''[/color] [b][color=navy])[/color][/b];
      s.type [color=blue]=[/color] [color=red]'text/javascript'[/color];
      s.setAttribute[b][color=navy]([/color][/b] [color=red]'src'[/color], [color=red]''[/color] [b][color=navy])[/color][/b];
      s.src [color=blue]=[/color] src;
      [color=darkorange]// The above is the most verbose but most compatible way to add properties[/color]
      [color=purple]document[/color].getElementsByTagName[b][color=navy]([/color][/b] [color=red]'head'[/color] [b][color=navy])[/color][/b].[b][color=navy]item([/color][/b] [color=red]0[/color] [b][color=navy])[/color][/b].appendChild[color=navy][b]([/b][/color] s [b][color=navy])[/color][/b];
    [b][color=navy]}[/color][/b]
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to clarify, I can call addScript('foo.js') and the page will "require" foo.js, including any variables it may define, etc.?

    At any rate, thank you very much for your assistance.

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that is correct.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •