iwato 19 Posted May 15, 2009 Report Share Posted May 15, 2009 QUESTION: Could someone please explain the use of the slash and vertical bar in the following statement:if (/loaded|complete/.test(document.readyState)) return initfb();Roddy Quote Link to post Share on other sites
jeffman 86 Posted May 15, 2009 Report Share Posted May 15, 2009 (edited) The unquoted pair of slashes creates a regular expression object. The expression in this case is looking for an instance of the strings "loaded" or "complete". The bar is a regex OR operator. (Not exactly, but close enough.) The dot followed by the test() method simply means that test() is a method of a regex object. It's a little funky to see a dot.property hanging off a forward slash, but it's legal. The writer could have done one of these instead:var re = /loaded|complete/;if (re.test(document.readyState)) return initfb();ORvar re = new RegExp("loaded|complete");if (re.test(document.readyState)) return initfb(); Edited May 15, 2009 by Deirdre's Dad Quote Link to post Share on other sites
chibineku 0 Posted May 15, 2009 Report Share Posted May 15, 2009 I love how elegant that is...you say funky, but I say neat. How does one come to learn the shorthand (shortcut?) methods that are a little off the beaten track? Quote Link to post Share on other sites
jeffman 86 Posted May 15, 2009 Report Share Posted May 15, 2009 I forget where I first saw that. Sometimes I look at scripts in pages that interest me. Firefox 3 Beta makes linking to script documents a lot easier. Sometimes I surf for javascript tricks and tips. Sometimes I'll spot something when I'm Googling a problem. It adds up. Quote Link to post Share on other sites
iwato 19 Posted May 16, 2009 Author Report Share Posted May 16, 2009 The dot followed by the test() method simply means that test() is a method of a regex object. It's a little funky to see a dot.property hanging off a forward slash, but it's legal.Thanks, Deirdre's Dad. I substituted the text that you suggested and the result was the same. I still have not fixed the problem, but at least my knowledge of Javascript has increased.Roddy Quote Link to post Share on other sites
jeffman 86 Posted May 17, 2009 Report Share Posted May 17, 2009 What is the problem? Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.