Results 1 to 7 of 7

Thread: What is error in this JavaScript code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Exclamation What is error in this JavaScript code

    Code:

    var X="http://www.vbzoom.com";
    if(X.match("http://")!="http://")
    {
    alert("Error Site.");
    }

  2. #2
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    I'm guessing that you want the alert box to be shown if the
    string X doesn't contain http://

    is so then you need to do the following.

    first - the match() method returns true or false so you don't need the
    !="http://"

    second - when using match you need to use a regular expression
    these are defined using / / to contain the regular expression
    (regex).

    so the correct use of match is :

    Code:
    string.match(/regex/)
    third - regex's can't contain special charachters such as /'s unless they are escaped with a \ so to use /'s they need to be coded as \/.

    so your correct code is :
    Code:
    var X="http:/www.vbzoom.com"; 
      if(!X.match(/http:\/\//)) 
      { 
        alert("Error Site."); 
      }
    note: we are using ! in front of the statement to reverse the outcome of the match method.

    So if the statment matches http:// then it returns true we then reverse it with ! so that it retuns false resulting in the alert not being displayed.

    Alternativley: if the match statement doesn't find http:// then it returns false we then use ! to turn the false into true resulting in the alert box being shown.

  3. #3
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    You'd probably be better off using indexOf in this case wouldn't you?

  4. #4
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    You mean rather than using ! on the outcome, as index of returns -1 if it's not found ?

    Personal preference really isn't it I suppose !

  5. #5
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Yep Although is RegExp slower than indexOf? I have no idea, but I suppose the difference would be so small it'd make no difference.

  6. #6
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    The advantage of indexOf is not speed, but whether or not "http://" comes at the begining of the line, or elsewhere. Since you are trying to test a URL for a certain protocol....

    'Course, you can do that in the RegEx with $.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  7. #7
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    note: If the 'http://' is at the start of the string indexOf will return 0 which is equivalent to false.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width