?
Solved

.js file

Posted on 2000-05-10
13
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

741 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