Solved

Accessing functions in javascript

Posted on 2014-02-08
9
381 Views
Last Modified: 2014-02-10
Hi Experts,

I want to access functions defined in javascript file (foo.js) from sample.js  file. These javascript files reside in the same folder.

[subtitle]foo.js[/subtitle]

function foo() {

}

foo.ftype.Isempty=function(){

}

//Returns a value in specific format

foo.Telformat=function(param){

}

//Returns true / false

foo.ValidateTel=function(param){

}

//Returns true / false

foo.Isnumber=function(param){

}

[subtitle]sample.js[/subtitle]

function testall (someparam)
{

//Here I need to call foo.js file helper functions

}

Open in new window

0
Comment
Question by:ksd123
  • 4
  • 3
  • 2
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39844030
to use function from another file you need to load it with your browser

<script src="/path/to/same/folder/foo.js"></script>
<script src="/path/to/same/folder/sample.js"></script>

Open in new window

0
 

Author Comment

by:ksd123
ID: 39844062
Basically foo.js contains generic functions that  will return boolean / some values.So in the sample.js file I have a function called testall and  want to invoke generic fucntions.

sample.js

function testall (someparam)
{

//Here I want to invoke / call the functions defined in the foo.js file 

}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 39844069
if you load the scripts as suggested in my first post, there's no problem
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:ksd123
ID: 39844078
I did n't get you. I am c# developer ,suppose if I have a class (common.cs) and have methods for  Format,reverse of string,Isnumber etc. I can invoke these methods in other classes .In sample.cs file  to access common methods , I will  instantiate common class and pass parameters to these  methods as required.In javascript ,How can I achieve this?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 200 total points
ID: 39844089
In JavaScript you must load the script in the page before the script using the fonction inside. You don't have to do anything else. Don't need to have a référence inside simple.JS.
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
ID: 39844290
@leakim971 is correct.  Javascript methods are different than something like C#.  C# (for web pages) runs on the server.  Javascript runs in the browser.  When a web page is opened that contains javascript, the browser must load all of the files because javascript itself isn't allowed to directly access files on the client or the server.  It can only work with what the browser has loaded.
0
 

Author Comment

by:ksd123
ID: 39845022
Mat be my question is not clear,let me put in this way actually I am doing model validation in angularjs and checking  user input is valid or not.Suppose if user enter's invalid data in the text box and  have a function in sample.js file that takes input data from form and checks valid or invalid.To do this I want to invoke common  methods(IsNumber,ValidateTel etc) and return true (no msg) or false (Show error msg in the UI).In this case, I don't have to do anything?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39845038
This page http://docs.angularjs.org/tutorial/step_02 shows part of how angularjs is used.  It show including the necessary javascript files in the web page so that the application code has access to the functions that are needed.  You do have to include the necessary files in script links so they will be loaded by the browser and be available.
0
 

Author Closing Comment

by:ksd123
ID: 39849418
Thank you guys
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

773 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