Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-16-2013, 03:42 AM   PM User | #1
DMN
New Coder

 
Join Date: Dec 2012
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
DMN is an unknown quantity at this point
Calling a Function in External JS File

Hi All, I'm very new to Javascript and would like some pointers. If I have multiple functions in the same external Javascript file, how would I call a particular function inside that external JS file from a web page. For instance suppose I have a JS file called myJSFunction.js and within this file I have three functions like the following:
Function1()
{
Do Something
}

Function2()
{
Do What Ever
}

Function3()
{
Do Stuff
}

How would I call Function2() from a web page? Any suggestion is greatly appreciated, thanks in advance.
DMN is offline   Reply With Quote
Old 03-16-2013, 04:24 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Question

Have you tried:

<script type="text/javascript" src="myJSFunction.js"></script>

Some place before the functions are called, but after elements of page have been loaded.
Usually just before the ending </body> tag.
jmrker is offline   Reply With Quote
Old 03-16-2013, 05:07 AM   PM User | #3
DMN
New Coder

 
Join Date: Dec 2012
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
DMN is an unknown quantity at this point
Hi, thanks for replying. I know about linking to an external JS file as you have mentioned above, but what I would like to know is after placing a link to the external JS file in the script element, can I then call Function1, Function2, or Function3 in the body of my page like the following:
Code:
<body>
      <div>
         <a href="javascript:Function1()">Click Here </a><br>
         <a href="javascript:Function2()">Click Here </a><br>
         <a href="javascript:Function3()">Click Here </a>
      </div>
</body>

Last edited by DMN; 03-16-2013 at 05:24 AM..
DMN is offline   Reply With Quote
Old 03-16-2013, 07:28 AM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Never attach JavaScript insiide the href attribute.

Best is to not jumble any of the JavaScript in the HTML at all and put ALL the JavaScript in the external file.

<a id="a1" href="nojs.htm">click here</a>

and in the external HTML

document.getElementById('a1').onclick = function() {..... ; return false;}

the code for your function goes in place of the ..... in the above command that is in the external JavaScript file.

You attach JavaScript in the external file to HTML in the web page using the id of the element you want to attach it to (or in some cases class names if the same code is to be attached to multiple elements

The code suggested by DMN was considered to be bad practice in Netscape 2 and worse practice in every browser since then.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-16-2013, 09:41 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by DMN View Post
How would I call Function2() from a web page?
You call (execute) the function when an event happens. For example:

Code:
<input type = "text" id = "mytext" onblur = "function1()">
<input type = "button" value = "Click Me" onclick = "function2()">
<img src="logo1.jpg" onclick="function3()">
<select id = "myselect" onchange = "function4()">
<img src="smiley.gif" alt="Smiley"  onmouseover="function5(this)" >
Is this what you mean?

I like fish and chips, but you can't get that there [Paris] - all you can get are snails' legs. - Commentator Talksport
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 03-16-2013 at 09:43 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:47 AM.


Advertisement
Log in to turn off these ads.