SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to include an external JS file within a JavaScript code?

    Hi people

    I am running a JavaScript code that needs to include an external JS file if some condition evaluates to true.

    I need something like:
    Code:
    <script language="JavaScript" type="text/javascript">
    var condition = ..
    if (condition == true) {
    <script language="JavaScript" src="http://www.mysite.com/file.js"></script>
    }
    </script>
    Obviously this doesn't work.
    Does anyone know how this can be done in JavaScript?

    thanks in advance
    Jason

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     
    <head>
    <script type="text/javascript">
    var condition = ..
    if (condition == true) {
    
    var th = document.getElementsByTagName('head')[0];
    var s = document.createElement('script');
    s.setAttribute('type','text/javascript');
    s.setAttribute('src','http://www.mysite.com/file.js');
    th.appendChild(s);
    }
    </script>
    </head>

  3. #3
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, this is exactly what I needed


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
  •