PDA

View Full Version : window.location without # but with anchor name?



akdream
01-06-2011, 11:04 PM
Hi,

I'm using the following code


var myPage = window.location;

to get the URL of the page and have it replacing
' + myPage + ' in a document.write.

If I have an anchor on a page

http://mydomain.com/folder/pagename.html#2

I would like the window.location to return

http://mydomain.com/folder/pagename.html2

hence without # but with the anchor name.

Is there any way I can have it eliminating the #? (Something like replace # with nothing, take it out or such?)

Thanks for any help you can provide...

jscheuer1
01-07-2011, 06:49 AM
Using window.location will always return a # if there is a hash in the URL.

Yes you can strip it, but you need to make sure it's a string first:


var myPage = (window.location.href).replace(/#/, '');

The href is always a string.

akdream
01-07-2011, 09:55 AM
Thank you very much John! :) that worked like a charm!