Solved

.js file

Posted on 2000-05-10
13
237 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now