Solved

Accessing functions in javascript

Posted on 2014-02-08
9
403 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 

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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article will show, step by step, how to integrate R code into a R Sweave document
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

617 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