views:

133

answers:

4

Whats Different between

   <!--#include virtual="JS.htm" -->  //note that in this method all JS files imported  like below in JS.htm

and

  <script src="myjavascript.js" type="text/javascript"></script> 
+3  A: 

In the first case the javascript file is included on the server side so it is included inline. In the second example the browser downloads the html file and then sends another request to the server for the javascript file.

stimms
+4  A: 

The <!--#include virtual="JS.htm" --> is server side include, won't work without certain configurations and is included by the server there and then already whereas javascript's way is loaded into the page.

Server Side Includes (SSI) is a simple interpreted server-side scripting language used almost exclusively for the web.

The most frequent use of SSI is to include the contents of one or more files into a web page on a web server. For example, a web page containing a daily quote could include the quote by placing the following code into the file of the web page:

With one change of the quote.txt file, all pages including the file will display the latest daily quote. The inclusion is not limited to files. In this very example, on a server that can run fortune, the output of it, a randomly selected quote, can also be included.

Server Side Includes are useful for including a common piece of code throughout a site, such as a page header, a page footer and a navigation menu. Conditional navigation menus can be conditionally included using control directives.

Source: WikePedia

Sarfraz
+2  A: 

First off the #include is used only in classic ASP and has no meaning in ASP.NET so you should retag your question. So when you include something it actually takes the contents of this file and puts it at the place where it should be included, so you end up with a single file downloaded to the client.

When you include a javascript file, the contents of the file are not placed inside the main script but the browser sends a separate request to fetch it. This is the prefered way of including javascript.

Darin Dimitrov
+2  A: 

The include (SSI -- server side include) puts whatever's in js.htm inline while the script tag references an exterior file without putting it inline.

While the include saves load time up front, it doesn't allow the javascript to be cached by the browser, meaning it can actually be slower in the long run.

Generally, I'd suggest using the script tag referring to an exterior file.

D_N