Jump to content

The Forward Slash And Vertical Bar


iwato

Recommended Posts

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...