Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing functions in javascript

Posted on 2014-02-08
9
Medium Priority
?
413 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
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?
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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