Matt, sorry to be disagreeable, but that doesn't sound right at all. If the name "functionName" is truly nonexistent, then referring to it directly is an error in any JavaScript implementation I've ever seen.
Here, try a test case: http://mg.to/test/undefun.html This is the JavaScript code, just like your example: <script type="text/javascript"> document.write( 'before<br/>' ); if( functionName ) functionName(); document.write( 'after<br/>' ); </script> In a browser that fails, the document should just display "Before". In a browser that succeeds, it will display both "Before" and "After". This test fails in IE7, Firefox 2, Opera 9, and Safari 3. I believe it will fail in *every* browser. Do you have a browser where this actually works? Is there something different about the test case that you used? I would be curious to try running your test. Anyway, Richard's method will work correctly: if( typeof functionName != 'undefined' ) { functionName(params); } (I used != instead of !== - it makes no difference here.) If you are testing specifically for a global function, you can simplify it to: if( window.functionName ) { functionName(params); } Or the equivalent which I like to use: window.functionName && functionName(params); -Mike > From: Matt Patenaude > > OK, so, I've got a weird iPhone problem. One of the web > applications I'm working on has to test for the existence of > a function, and if it exists, execute it. > > On a normal browser, I'd do something like this: > > if (functionName) functionName(params); > > And it works perfectly on every browser I've tried, including > Safari 3 Beta. However, it not only fails on the iPhone, but > appears to trigger some sort of error. I say this, because > the following: > > alert('test one'); > if (functionName) functionName(params); > alert('test two'); > > ... only shows 'test one'. Since execution stops, that leads > me to believe something goes pretty foul somewhere. I've also > tried window.functionName, assigning it to the containing > object (then using "this.functionName"), and a few things > with the "typeof" operator, to no success. > > Anyone know why this fails, or more importantly, how I might > circumvent it on the iPhone beyond hard-coding my function names? > > Thanks! > > -Matt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "iPhoneWebDev" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/iphonewebdev?hl=en -~----------~----~----~----~------~----~------~--~---
