iwato Posted May 15, 2009 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 Link to comment Share on other sites More sharing options...
jeffman Posted May 15, 2009 Share Posted May 15, 2009 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(); Link to comment Share on other sites More sharing options...
chibineku Posted May 15, 2009 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? Link to comment Share on other sites More sharing options...
jeffman Posted May 15, 2009 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. Link to comment Share on other sites More sharing options...
iwato Posted May 16, 2009 Author 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 Link to comment Share on other sites More sharing options...
jeffman Posted May 17, 2009 Share Posted May 17, 2009 What is the problem? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.