views:

14

answers:

2

Hi, I have a problem with my JS script. I have written a function in my Site.Master because I use it in every page of my website. Right now I need to add a new line to that function (execute another function) but, and this is the whole problem, the function to be executed is only defined in one of my content pages.

To be honest it doesn't even cause any problems for me, everything works correctly, but I don't want to leave a call to the non-existing function in the rest of my content pages. So what I need is to define somehow that the last line of my master page function well be only executed if the page I am on is the one with the definition of that function.

Any ideas how to do that?

I am sorry for so confusing explanation of the problem...

A: 

You could try the following:

if (typeof MyFunction == 'function')
{
    MyFunction();
}

This will only call MyFunction if it exists on the current page.

Ben Robinson
worked perfectly, thanks :)
Tromax
A: 

Just check to see if the function is there before you call it. if (typeof foo != 'undefined') foo()

Matt Briggs