• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

javascript file include other javascripts links?

Can a javascript file include links to other javascript files?
0
Tom Knowlton
Asked:
Tom Knowlton
  • 5
  • 5
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now