Jump to content
iwato

The Forward Slash And Vertical Bar

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 :)

Share this post


Link to post
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();

Edited by Deirdre's Dad

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...