Solved

.js file

Posted on 2000-05-10
13
244 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

717 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