views:

188

answers:

4

Can someone help me with url hash? I use this code to get the current url and then send the data as json, but the hash part seems to be stripped then. i tryed to use href and hash and i always get the default.aspx but no hash part like #anchor.

var location = String(window.location);
var title = String(document.title);
var text = { 'url': location, 'title': title };

Thanks for help guys. I figured it out. All answers were correct as well as my code. I just put it in wrong event hadler. Because my hash parts are generating dynamically. Sorry for disturbing you guys!!!

+1  A: 

Have you tried using window.location.href instead of window.location?

window.location is an object, window.location.href is a string.

Andy Shellam
then im getting http://localhost:2217/Default.aspx
ilkin
+1  A: 

I'm not sure by the comment in your question, but did you try window.location.hash?

ZIP Code Database
yes i tried window.location.hash
ilkin
then im getting http://localhost:2217/
ilkin
+2  A: 

Try looking at the window.location object. It has these properties:

assign: function () { [native code] }

hash: "#test"

host: "stackoverflow.com"

hostname: "stackoverflow.com"

href: "http://stackoverflow.com/questions/2538697/getting-parts-of-url-window-location#test"

pathname: "/questions/2538697/getting-parts-of-url-window-location"

port: ""

protocol: "http:"

reload: function () { [native code] }

replace: function () { [native code] }

search: ""

Matt
+2  A: 

I think you must be doing something wrong. You're not showing us all the code involved. Here is a test page: http://gutfullofbeer.net/location.html#something

When you click that link, what do you see?

Pointy
thanks for the link.. sorry guys. i was doing wrog
ilkin