javascript file include other javascripts links?

Can a javascript file include links to other javascript files?
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
 
leakim971PluritechnicianCommented:
try it first inside your page, if it work fine, move it to an external file without any modification, it will work
again, the external file don't contains the script tags
0
 
sjklein42Commented:
One good technique that works by generating a script tag dynamically::

http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm
0
 
leakim971PluritechnicianCommented:
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");

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Tom KnowltonWeb developerAuthor Commented:
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?
0
 
leakim971PluritechnicianCommented:
no you can't
0
 
Tom KnowltonWeb developerAuthor Commented:
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>

0
 
leakim971PluritechnicianCommented:
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
0
 
Tom KnowltonWeb developerAuthor Commented:
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>
0
 
Tom KnowltonWeb developerAuthor Commented:
thx
0
 
leakim971PluritechnicianCommented:
don't forget to respect order

the dependencies first
0
 
Tom KnowltonWeb developerAuthor Commented:
>>>>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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.