www.webdeveloper.com
Results 1 to 7 of 7

Thread: Multiple Scripts in one external file...

  1. #1
    Join Date
    Jun 2009
    Posts
    20

    Multiple Scripts in one external file...

    Hi,

    I am tied to using a CMS that doesn't allow me to edit within the head tags but has a single preconfigured file that I can put any Javascript.

    The site I am currently building will have a couple of different instances where Javascript will be needed.

    For example, it will have a script controlling a drop down menu, and a script (making use of Jquery) that controls a tootip.

    So, making use of one external .js file, how do you suggest I call different scripts for different tasks, and I will also need to call the Jquery library.

    I would rather avoid including any js within the body tags.

    Thanks for any help

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    If you are not able to edit between the head tags you will need to include script tags in the body. There is no other way to do it given your restrictions.

  3. #3
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    You could try something like this which does work. You may have to create and attach more than one script element in the function, and be sure not to overwrite any existing onload handler.
    Code:
    (function()
    {
     var sTag = document.createElement('script');
    
     sTag.src = 'jquery.js';
    
     sTag.type = 'text/javascript';
    
     window.onload = function(){ /*Initialise scripts here */ };
    
     document.getElementsByTagName('head')[0].appendChild(sTag);
    
    })();

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    expanding on Ali's code, here is a reusable function:

    Code:
    (function(){
    
    function addJS(turl, timeout) {
    	var xJs = document.createElement("script");
    	document.getElementsByTagName("head")[0].appendChild(xJs);
    	xJs.src = turl;
    	return xJs;
    }
    
    
    addJS("jquery.js");
    addJS("jqueryui.js");
    addJS("hoverfx.js");
    
    
    }());
    you don't really need to bother with an onload event, and doing so would break jQuery's $.ready functionality...

  5. #5
    Join Date
    Jun 2009
    Posts
    20
    OK thanks for your help.

    so would I put the given script into the one javascript area that I have been allowed access too and the other .js files into the same directory as that file?

    cheers

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by mr dobalino View Post
    OK thanks for your help.

    so would I put the given script into the one javascript area that I have been allowed access too and the other .js files into the same directory as that file?

    cheers
    yes, in the same folder when using just file names.
    you can also specify full urls (http:// to .js), they don't even have to be on your site...

  7. #7
    Join Date
    Jun 2009
    Posts
    20
    Great thanks,

    And do I need to call that function from within script tags in the body? Would I need to put the following? Also, does it matter where I put this in the body?

    <script type="text/javascript">
    <!--
    onload = function ()
    // -->
    </script>

    sorry, thanks for you patience and help, I'm fairly new to this JS game as you can tell...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles