By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Basic Question on calling javascripts

P: n/a
JCO
I have several JavaScripts in a web page. Each script is in an individual
file with extension .js.
I want to know if it is possible to put all functions in one single page.
If so, how do I call the specific script that I want to run.

This is how I call an individual script:
<SCRIPT LANGUAGE="JavaScript" src="valForm.js"></SCRIPT>

Where valForm is a script that looks like this:
function valForm()
{
if( document.frmPassword.txtInput.value == "password" )
document.location.href="Private.htm";
else
document.location.href="NoAccessPage.htm";
}
If a single script contains 5-10 functions, how do you call the specific
function you want?

Thanks in advance.

Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
I have read the following message from " JCO" <J.********@verizon.net>
and have decided to lend my vast knowledge.

The writer said:
I have several JavaScripts in a web page. Each script is in an individual
file with extension .js.
I want to know if it is possible to put all functions in one single page.
If so, how do I call the specific script that I want to run.

This is how I call an individual script:
<SCRIPT LANGUAGE="JavaScript" src="valForm.js"></SCRIPT>

Where valForm is a script that looks like this:
function valForm()
{
if( document.frmPassword.txtInput.value == "password" )
document.location.href="Private.htm";
else
document.location.href="NoAccessPage.htm";
}
If a single script contains 5-10 functions, how do you call the specific
function you want?

Thanks in advance.

and my reply is:
The name of a js file has nothing to do with the name of the function
inside. In fact a script may not even contain a function. You can one
or multiple js files depending on how you use them. The browser loads
them all into the page and does't care where they came from. Just name
them anything you want.
function xxx() {
....
};
function yyy() {
....
};

function zzz() {
....
};
Put the above in 1, 2, or 3 js files. It doesn't matter.
To use a function just refer to it's name
xxx() or yyy() or zzz()

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #2

P: n/a
" JCO" <J.********@verizon.net> wrote in message
news:m2*****************@nwrddc01.gnilink.net...
I have several JavaScripts in a web page. Each script is in an individual
file with extension .js.
I want to know if it is possible to put all functions in one single page.
If so, how do I call the specific script that I want to run.

This is how I call an individual script:
<SCRIPT LANGUAGE="JavaScript" src="valForm.js"></SCRIPT>

Where valForm is a script that looks like this:
function valForm()
{
if( document.frmPassword.txtInput.value == "password" )
document.location.href="Private.htm";
else
document.location.href="NoAccessPage.htm";
}
If a single script contains 5-10 functions, how do you call the specific
function you want?

Thanks in advance.


Just merge them into a single include file and run it.

Nothing else has to change unless, perhaps, if there are duplicate function
names or global variables declared.
Functions are called in numerous ways:

<body onload="func1()">

<form onsubmit="return func2()">

<img src="button.gif" onclick="func3()">

and, of course, from within other functions.
Does this help?
Jul 20 '05 #3

P: n/a
Lee
JCO said:

I have several JavaScripts in a web page. Each script is in an individual
file with extension .js.
I want to know if it is possible to put all functions in one single page.
If so, how do I call the specific script that I want to run.
Your terminology is likely to cause confusion at some point.
A program written in C++ is not "a C++".
A script written in JavaScript is not "a JavaScript".

This is how I call an individual script:
<SCRIPT LANGUAGE="JavaScript" src="valForm.js"></SCRIPT>
That's not really "calling" anything. That's telling the browser
where to load a block of JavaScript from. The browser loads the
code block into the current page, making the functions available
and executing any in-line code they may contain.

Where valForm is a script that looks like this:
function valForm()
{
if( document.frmPassword.txtInput.value == "password" )
document.location.href="Private.htm";
else
document.location.href="NoAccessPage.htm";
}
If a single script contains 5-10 functions, how do you call the specific
function you want?


The only way to call a function is to invoke it by name,
as in "valform()". It doesn't matter where the browser
loads it from. It's either loaded into the current page,
or it isn't.

Jul 20 '05 #4

P: n/a
JCO
Thanks all three for your reply. I guess I was getting confused because in
my example, the name of the js script is also the name of the file source.
Now I realized that the source file is loaded one time which makes all of
the functions available to my website.

Very cool! Thanks again
"Lee" <RE**************@cox.net> wrote in message
news:c0********@drn.newsguy.com...
JCO said:

I have several JavaScripts in a web page. Each script is in an individualfile with extension .js.
I want to know if it is possible to put all functions in one single page.
If so, how do I call the specific script that I want to run.


Your terminology is likely to cause confusion at some point.
A program written in C++ is not "a C++".
A script written in JavaScript is not "a JavaScript".

This is how I call an individual script:
<SCRIPT LANGUAGE="JavaScript" src="valForm.js"></SCRIPT>


That's not really "calling" anything. That's telling the browser
where to load a block of JavaScript from. The browser loads the
code block into the current page, making the functions available
and executing any in-line code they may contain.

Where valForm is a script that looks like this:
function valForm()
{
if( document.frmPassword.txtInput.value == "password" )
document.location.href="Private.htm";
else
document.location.href="NoAccessPage.htm";
}
If a single script contains 5-10 functions, how do you call the specific
function you want?


The only way to call a function is to invoke it by name,
as in "valform()". It doesn't matter where the browser
loads it from. It's either loaded into the current page,
or it isn't.

Jul 20 '05 #5

P: n/a
I recently learned that language="javascript" is deprecated in favor of
type="text/javascript"

Just a remark.
--
Bas Cost Budde
http://www.heuveltop.org/BasCB
but the domain is nl

Jul 20 '05 #6

P: n/a
JCO
Yes I have noticed that too. I made the changes.
However, can you tell me the difference in the code below:

<FORM NAME="frmPassword" onSubmit="return valForm()">
<FORM NAME="frmPassword" onEnter="return valForm()">

"Bas Cost Budde" <ba*@heuveltop.org> wrote in message
news:c0**********@news2.solcon.nl...
I recently learned that language="javascript" is deprecated in favor of
type="text/javascript"

Just a remark.
--
Bas Cost Budde
http://www.heuveltop.org/BasCB
but the domain is nl

Jul 20 '05 #7

P: n/a
JCO wrote:
However, can you tell me the difference in the code below:

<FORM NAME="frmPassword" onSubmit="return valForm()">
<FORM NAME="frmPassword" onEnter="return valForm()">


Er, the first line uses the Submit event? That shall not be your question.

(reads the HTML 4.01 spec) I can't find an onEnter property. What is
that supposed to mean?

--
Bas Cost Budde
http://www.heuveltop.org/BasCB
but the domain is nl

Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.