1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Javascript

Discussion in 'Web Design & Development' started by bob121, Sep 29, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. bob121

    bob121 Thread Starter

    Joined:
    Sep 10, 2006
    Messages:
    207
    Hello, is there any reason why this piece of Javascript works in Firefox but niether of the Internet Explorers.

    Code:
    function gup(){
    var regexS = "([\\#][^]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec( window.location.href );
    if(results === null) return "";
    else return results[0].replace("#","");
    } 
    
     
  2. namenotfound

    namenotfound

    Joined:
    Apr 30, 2005
    Messages:
    3,012
    This is one of the drawbacks of client-side scripting. I've seen some websites with different JavaScript tailored toward each browser, making the page very bulky.
     
  3. lumentec

    lumentec

    Joined:
    Mar 23, 2008
    Messages:
    103
    I don't really like the syntax of your javascript without {}'s and all. If at any benefit, I've added some formatting and syntax editing.

    Code:
    function gup() {
         var regexS = "([\\#][^]*)"
     ,regex = new RegExp(regexS)
     ,results = regex.exec( window.location.href );
    
              if (results === null) {
                   return "";
              }
    
              else {
                   return results[0].replace("#","");
              }
    }
     
  4. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Whoever wrote that code should be crucified.

    1. You can just use location.hash, problem solved.

    2. crap code: if location.hash didn't exists you could just use this:

    PHP:
    function gup() {
        var 
    location.href.indexOf('#');
        return ~
    location.href.substr(1) : null;
    }
    (don't use this, i'm just saying. Use location.hash)


    Jamie Zainski really hit the nail on the head:

     
  5. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    Thanks for the code snipet. I didn't know JavaScript support that C syntax. I just learned something new today. :)

    Peace...
     
  6. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    I assume you are referring to the ternary operator, yes, its quite useful. :)
     
  7. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    Yep. :)

    Peace...
     
  8. bob121

    bob121 Thread Starter

    Joined:
    Sep 10, 2006
    Messages:
    207
    Thanks very much guys ! :)

    I noticed that you said do not use the location.hash script, If i don't use it what do i use ?
     
  9. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Use location.hash, not the gup() function.
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/754596

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice