Solved

Accessing functions in javascript

Posted on 2014-02-08
9
378 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 82

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 82

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now