Calling Sub-Function in Javascript Different in Safari

Posted May 11, 2006 at 7:47 AM

Tags: Javascript / DHTML

I just came across a browser inconsistency that I have never seen before. Granted I am new to this one aspect, but still, interesting. I had an example of code that had a sub function being called in this manner:

 Launch code in new window » Download code as text file »

  • // This will NOT work in safari.
  • function Foo2(){
  • alert("In Foo2 Method");
  •  
  • (
  • function Bar2(){
  • alert("In Bar2 Method");
  • }
  • )();
  • }
  •  
  • // Call the Foo2 method for testing.
  • Foo2();

In the above example, we have a sub-function that is being called via the notation ( .... )(); This notation is used for nameless functions. In this example the function does have a name, "Foo2." In IE and FireFox, this still works.

If you want to keep this notation and have it work in Safari, you cannot use a function name, as in this example:

 Launch code in new window » Download code as text file »

  • // This will work in Safari.
  • function Foo(){
  • alert("In Foo Method");
  •  
  • (
  • function (){
  • alert("In Bar Method");
  • }
  • )();
  • }
  • Foo();

In the above example, you will see that the function call is the same ( ... )(); the only difference is that the sub-function is no longer named. It is merely the function definition "function()". This works fine so long as you don't need to refer to the function by name, say as you might in a recursive method call.

In order to recurse, you need the name and you need to change the notation a bit. To use a named function in Safari, you need to call the function explicitly as below:

 Launch code in new window » Download code as text file »

  • // This will work in Safari.
  • function Foo(){
  • alert("In Foo Method");
  •  
  • function Bar(){
  • alert("In Bar Method");
  • }
  •  
  • Bar();
  • }
  • Foo();

In the above example, you see that we need to call the Bar method explicitly with Bar();. Additionally, we no longer use the ( ... )(); notation and this does work in Safari and the other browsers.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »