PDA

View Full Version : Calling a function in javascript



mohtasham1983
March 4th, 2008, 08:06 PM
Hi,

I've been working with YUI (Yahoo user Interface) javascript library for a while without much knowledge about Javascript itself. However, I have learned a lot about javascript when working with YUI.

Yesterday, I came across something that I wanted to do with javascript so that I don't need to rewrite a function again.

I have a function which i found in YUI examples like this which does its job very fine:



YAHOO.util.Event.addListener(window, "load", function() {
YAHOO.example.Local_XML = new function() {
...
...
...
...
};
});

As you can see once the page is loaded this function is executed. Now, what I'm looking for is to find a way of calling the same function again in another event outside my function.

For example I have the following function:



function new_function() {
....
...
I need to call YAHOO.example.Local_XML function and its method in here

}

I would be very glad if somebody let me know how this feature of javascript works and give me some guide lines about it.

Thanks

LaRoza
March 4th, 2008, 08:10 PM
The syntax "= function(){}" or "= new function(){}" are anonymous, and cannot be called by name, which is their point. They the are meant to be single use functions, like lambda's.

You can just trigger the event again to call it, or put the function in an named function and just call that function for both events.

mohtasham1983
March 4th, 2008, 08:20 PM
Thanks, it was really helpful. I just made it as a simple function and it worked fine.

I just don't know why yahoo developers used that in their example page.

LaRoza
March 4th, 2008, 08:58 PM
Thanks, it was really helpful. I just made it as a simple function and it worked fine.

I just don't know why yahoo developers used that in their example page.

Most events use anonymous functions. That is where you will see them most often.

In my experience, most of the time you don't use the same function for different events, so it works fine.

mohtasham1983
March 4th, 2008, 09:08 PM
In my case I have a div element which will be filled by a datatable whose data is retrieved from an xml file in the load event.

Inside the datatable I have a button which deletes one of the xml elements. I just needed to call the same function to retrieve data from xml document after that element was deleted without loading the entire page again.