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 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »