Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on 

javascript file include other javascripts links?

Can a javascript file include links to other javascript files?
JavaScript

Avatar of undefined
Last Comment
Tom Knowlton
Avatar of sjklein42
sjklein42
Flag of United States of America image

One good technique that works by generating a script tag dynamically::

http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm
Avatar of leakim971
leakim971
Flag of Guadeloupe image

simple js :

var script = document.createElement("script");
script.src = "/path/to/other/javascript/somejavascript.js";
document.head.appendChild(script);

jQuery : http://api.jquery.com/jQuery.getScript/

$.getScript("/path/to/other/javascript/somejavascript.js");

Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

Why are these solutions so convoluted?

Can't I just do this:

------------------------------------------
top of file named somejavascript.js
------------------------------------------

<script src="../../Scripts/anotherscript.js" type="text/javascript"></script><script <src="../../Scripts/andyetanotherscript.js" type="text/javascript"></script>

function()
{

}

----------------------------------------------
bottom of flie named somejavascript.js
----------------------------------------------



Then in some html page somewhere we do:

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



Would this work?
Avatar of leakim971
leakim971
Flag of Guadeloupe image

no you can't
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

Let say I have the following in my HTML <head> section:


<script type="text/javascript">

    $(document).ready(function ()
    {
        $('select').change(function ()
        {
            $('#waitanimationtext').text(" Retrieving Data - Please wait...");
            $('option').throbber({ parent: "#waitanimation", image: "ajax-loader.gif" });
        });
    });
    
</script>

Open in new window

\

Can I take everything that is between the <script></script> tags and move it to an external file called textbookthrobber.js  (for example)

and then just inlclude textbookthrobber.js like this?

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


and would it work as long as I had the following declarations ABOVE that include:

<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.throbber.js" type="text/javascript"></script>

Avatar of leakim971
leakim971
Flag of Guadeloupe image

yes of course but please not textbookthrobber.js don't contains script tag, it contains only the javascript code

and you need to place <script src="../../Scripts/textbookthrobber.js" type="text/javascript"></script> after the jQuery plugin because it use jQuery
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

Assuming that this would work, could I create a bunch of external files for use with jquery...like ths?

external file nameavailthrobber.js
 $(document).ready(function ()
    {
  $('input[id*="ButtonCheckAvail"]').mousedown(function ()
        {
            $('#waitanimationtext').text(" Checking Name Availability - Please wait...");
            $(this).throbber({ parent: "#waitanimation", image: "ajax-loader.gif" });
        });
  });

Open in new window


external file passwordcheckthrobber.js
 $(document).ready(function ()
    {
  $('input[id*="ButtonChangePW"]').mousedown(function ()
        {
            $('#waitanimationtext').text(" Changing Password - Please wait...");
            $(this).throbber({ parent: "#waitanimation", image: "ajax-loader.gif" });
        });
  });

Open in new window




Then back in the main HTML page I would go:

<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.throbber.js" type="text/javascript"></script>
<script src="../../Scripts/nameavailthrobber.js" type="text/javascript"></script>
<script src="../../Scripts/passwordcheckthrobber.js" type="text/javascript"></script>
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

thx
Avatar of leakim971
leakim971
Flag of Guadeloupe image

don't forget to respect order

the dependencies first
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

>>>>don't forget to respect order

the dependencies first

==================================

Understood.

So far it is working just great.....helps to organize it some....some of the js is for specific clients, so it is nice to be able to break it up like this.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo