Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accessing functions in javascript

Posted on 2014-02-08
9
Medium Priority
?
420 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
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!

 

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 800 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 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 84

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

963 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