Solved

.js file

Posted on 2000-05-10
13
242 Views
Last Modified: 2012-05-04
Hi Experts
I would like to know the concept of .js file.
Is it like a include file?
Is it cached by the browser?
How do i wirte it?
Can i have all my javascript functions in it?

Any information /links /sites will be of help.

Thnx
Kunal
0
Comment
Question by:jkunal
  • 5
  • 5
  • 3
13 Comments
 
LVL 1

Expert Comment

by:yatest5
ID: 2799339
Kunal,

You're in the wrong question area here - JavaScript has nothing to do with Java.  But never mind...

Javascript functions can be placed in HTML within <SCRIPT></SCRIPT> tags, but sometimes it is nice to separate functionality from the HTML.  This can be accomplished by putting all the JavaScript code in a .js file (without the SCRIPT tags) and referencing it in the HTML as

<SCRIPT SRC=myfile.js>
</SCRIPT>

The advantages of this are that you can reuse code in several different web pages and also that people viewing the source of the page cannot see you JavaScript functions.

There's not much more to say than that - but a useful JavaScript tutorial page is

http://www.tactika.com/realhome/realhome.html 

Good luck,

Tom
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 2799554
1. Yes, a js file is indeed like an include file, you can use it in many of your pages by just including it
2. Yes, when the page is viewed it finds out there is an js file which should be loaded, it loads the file and keeps it in it's memory...(not really cached!!)...
3. You write it just like you normally write your javascript functions. But without the <script><!-- and //--></script> tags.
4. yes, all of your javascript functions.

to include the js file, you do:
<script src="yourjsfile.js">
</script>

and you can use all the functions.

Good luck coding!
CJ


0
 
LVL 4

Author Comment

by:jkunal
ID: 2800707
Thnx CJs, but how do i call those functions at the client.When i try calling it like the normal function it gives an object required error.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:yatest5
ID: 2800741
EXAMPLE

HTML FILE (whatever.html)

<HTML>
<BODY onLoad=hello()>
yada yada
<SCRIPT src="script.js">
</SCRIPT>
</BODY>
</HTML>

JS FILE (script.js)

function hello()
{
   alert('hello');
}

Put these in the same directory and load the HTML to see .js files in action.

Cheers,

Tom

0
 
LVL 22

Accepted Solution

by:
CJ_S earned 70 total points
ID: 2800751
Ok, a li'l sample. The .js file:

// start of js file, test.js

function showM(Str)
{
   alert(Str);  
}

// End of js file


// html file

<html>
<head>
<script src="test.js">
</script>
</head>

<body>
<script language=javascript>
<!--
   showM("hi");
//-->
</script>

</body>

// end of html file
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 2800763
huh, he wrote the same as I did *LOL*
0
 
LVL 4

Author Comment

by:jkunal
ID: 2803162
Thnx Cjs and yatest5 for your inputs.
0
 
LVL 1

Expert Comment

by:yatest5
ID: 2803740
MOAN MOMENT

How come I gave exactly the same answer as CJ_S BEFORE him and didn't get my answer accepted?

MOAN MOMENT OVER

Never mind,

Cheers,

Tom
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 2803771
probably because that was only an example of what I explained before>??
0
 
LVL 1

Expert Comment

by:yatest5
ID: 2803797
That is true, but if you look above what you wrote before - I have written almost exactly the same previously to your comment.

Whatever.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 2803859
true, within 1 minute I posted the same...

never mind,
0
 
LVL 4

Author Comment

by:jkunal
ID: 2803951
Hi yatest5

The points were not only for that post but also for the one before that(Thursday, May 11 2000 - 03:26PM IST).
Anyway i should have shared points but, then i think EE should provide that kinda functionality.

I am sorry if i hurt you.

Kunal
0
 
LVL 1

Expert Comment

by:yatest5
ID: 2803963
Don't worry about it - I'm OK.

Tom

PS I'm turning off e-mail notification for this question now.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question