...

View Full Version : Way to get stuff after url's "#" in javascript?



harjanto
07-18-2009, 02:30 PM
Say a user types in the url http://localhost/#dog

How would you get the string "dog" from the url with javascript? Does the method with all major browsers (ie6+, firefox, safari, chrome)?

Thanks for the help

venegal
07-18-2009, 03:28 PM
window.location.hash is what you're looking for. It's IE4+, FF1+.

Philip M
07-18-2009, 03:29 PM
var url = "http://localhost/#dog";
var stub = url.replace(/http.+#/,"");
alert (stub);


“Going to church does not make you a Christian anymore than going to the garage makes you a car.” - Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)

coothead
07-18-2009, 04:28 PM
...or maybe this?...


var url = "http://localhost/#dog";
var stub = url.split('/#')[1];
alert (stub);

coothead

venegal
07-18-2009, 05:02 PM
Or maybe you use the builtin function instead of rolling your own.

You will have to access window.location anyway in order to get to the URL, so why not just use its own hash method.

Philip M
07-18-2009, 05:40 PM
Or maybe you use the builtin function instead of rolling your own.

You will have to access window.location anyway in order to get to the URL, so why not just use its own hash method.

My understanding is that the OP wishes to extract the string dog, not #dog.

barkermn01
07-18-2009, 07:38 PM
there is a Javascript function so that $_GET[] works in javascript, so you could use $_GET[] after a # e.G #dog=dog, and then you can use the $_GET to get them its how i do it



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum