Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Welcome to the TechnoWorldInc! Community!
Recent Updates
[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » Javascript
 Using External JavaScript Files
Pages: [1]   Go Down
  Print  
Author Topic: Using External JavaScript Files  (Read 2061 times)
Daniel Franklin
TWI Hero
**********


Karma: 3
Offline Offline

Posts: 16647


View Profile Email
Using External JavaScript Files
« Posted: September 29, 2007, 10:56:08 AM »


When you have lots of JavaScript functions and you plan to use them on many web pages, it is always advisable to keep them in a separate file, and then include that file on every page where those functions are required. This way, you can better organize them, you don't have to copy/paste the code again and again (imagine if it runs to 100 or more lines!!), and you'll be forced to write more generalized code for better integration and scalability.

Besides, enclosing the common JavaScript routines in external files keeps your web pages uncluttered and search engine spiders and crawlers are able to reach the important content easily. They don't have to shuffle through tens of lines of scripts.

In the first edition [that appeared in my newsletter - BYTESWORTH REACHOUT] of my article I had omitted the process of creating the external JavaScript files, but when one of my readers asked me how to do it, I had to re-write the article.

It's very simple. Use any text editor of your liking (if you hand-code your HTML, you can use the same editor that you use for your HTML editing). Write the required code, and save the file with .js (dot js) extension.

I'm going to give you a small example.

Suppose your JavaScript file is "myjfuncs.js".

and it has a function to return the bigger number of the two supplied as parameters (you can simply start with the function, no need to write <script> or some other stuff):

function bigger(num1, num2) { var holdbig; if (num1<num2) { holdbig=num2; } else if(num1>num2) { holdbig=num1; } else { holdbig=num1; } return holdbig; }

In a similar manner, we can have


function smaller(num1, num2) {

}

When we want to use this precious file full of JavaScript functions, we use this line:

<script language="JavaScript" type="text/javascript" src="myjfuncs.js"> </script>

So when we use one of its functions in a JavaScript:

<script language="javascript">

var n1=90; var n2=7; alert ("The bigger number is: " + bigger(n1, n2));

</script>

In this way, we can use all the complex functions contained in the myjfuncs.js file.

Articles Source - Free Articles

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.087 seconds with 24 queries.