Javascript

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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("#","");
}
 
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.
 
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("#","");
          }
}
 

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 t = location.href.indexOf('#');
	return ~t ? location.href.substr(t + 1) : null;
}
(don't use this, i'm just saying. Use location.hash)


Jamie Zainski really hit the nail on the head:

http://en.wikiquote.org/wiki/Jamie_Zawinski said:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
 

tomdkat

Retired Trusted Advisor
Joined
May 6, 2006
Messages
7,148
PHP:
function gup() {
	var t = location.href.indexOf('#');
	return ~t ? location.href.substr(t + 1) : null;
}
Thanks for the code snipet. I didn't know JavaScript support that C syntax. I just learned something new today. :)

Peace...
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
Thanks for the code snipet. I didn't know JavaScript support that C syntax. I just learned something new today. :)

Peace...
I assume you are referring to the ternary operator, yes, its quite useful. :)
 

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 ?
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Members online

Top