Calling Sub-Function in Javascript Different in Safari

Posted May 11, 2006 at 7:47 AM by Ben Nadel

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:

  • // 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:

  • // 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:

  • // 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.



Reader Comments

There are no comments posted for this web log entry.

Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 22, 2013 at 11:37 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
When I c&p and run on cf10, I get: Selected User IDs: 1,4 User 1 selected: YES - YES User 2 selected: NO - NO User 3 selected: NO - NO User 4 selected: YES - YES User 5 selected: NO - ... read »
May 22, 2013 at 11:27 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Tom, Good thought, but no dice. Both of these still exhibit the same behavior: users.id[ users.currentRow ] users[ "id" ][ users.currentRow ] It's just something whacky happening with ... read »
May 22, 2013 at 11:07 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
Could your problem be that "users.id" is actually an ARRAY, not a single value? Perhaps try it again with "users.id[1]" (I only have CF8 here at work). ... read »
May 22, 2013 at 7:52 AM
Nested Views, Routing, And Deep Linking With AngularJS
Hi, Just a quick thank you. As it happens, for my own purposes, the pending ui-router work being done in native angular is likely the one I'll adopt, but your exploration, code and documentation of ... read »
May 22, 2013 at 4:43 AM
How Do You Use The ColdFusion CFParam Tag?
'<cfparam>' or 'isDefined()and <cfset>' performs the same task.Is there any difference? ... read »
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools