Check if a Javascript Function Exists or Is Defined

Jscript

ssd-virtual-servers-banner-468x60

It’s always frustrating when you get an error trying to call a function that hasn’t been defined but there’s an easy way  to prevent this. To check if a Javascript function exists before calling it, try this:

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

Bloggers: WP Engine has increased their referral payments to $200 each! Try out their WordPress hosting. You might also be interested in my post on top affiliate programs for bloggers or these other projects:

Also, check out Publishing with WordPress, it’s full of useful and timely WordPress guides,  or follow me on Twitter @reifman.

  • Tommy

    Finally! I’ve been looking for way too long for just this easy a solution! Thank you very very much!!!!

  • Bob

    terrific, you are on 1st place in google for javascript check it function exists, thank you

  • http://twitter.com/darinreid Darin Reid

    Hey neighbor, imagine my surprise when I googled this tonight. See you at coffee!

    • jeffreifman

      HA! We should cowork :) Email me your email again … as I can’t seem to find it.

  • MattC

    very nice. Simple an to the point – just what I was looking for. :)

  • http://www.facebook.com/neil.young1 Neil Young

    Great stuff, worked a treat – thanks for this!

  • Lutz Zimmmermann

    Great stuff, thanks a million!

  • Ken

    Jason Bunting’s answer on Stackoverflow is probably the better way to do this. http://stackoverflow.com/questions/85815/how-to-tell-if-a-javascript-function-is-defined

  • Nicola

    Thanks!

  • Rolly Schicker

    Sounds like y’all are real happy with the solution…now for us dummies….where do you enter(type) all that good stuff?

  • Nikola

    Man you have been a great help, thank you for this trick

  • http://www.i-ray.nl/ ray

    great solution thx!

Top
Read previous post:
How to speed up your WordPress blog with Apache, Varnish and W3 Total Cache
Making WordPress Faster with Apache, Varnish and W3 Total Cache on Amazon AWS EC2 with CloudFront CDN

Update: There is now a much more complete tutorial to installing Varnish with W3 Total Cache and Amazon CloudFront here....

Close