web-tiki
web-tiki

Reputation: 103810

Jquery use a variable for a load query

I have a variable but I can't use it the way I want, please help!!

var test = window.location.hash;
$('div').load("test.php?id="+test);

The request keeps on being :

XHR finished loading: "http://localhost/test-site/test.php?id=". 

and ignores my variable...

Upvotes: 2

Views: 87

Answers (2)

Blazemonger
Blazemonger

Reputation: 92983

window.location.hash will begin with a # symbol, if it contains anything at all. You should strip it by adding .substr(1):

var test = window.location.hash.substr(1);
$('div').load("test.php?id="+test);

As it is, you are trying to load a url like test.php?id=#22, and since the hash is meaningless for AJAX purposes, it's being ignored by the .load method.

Upvotes: 3

Emil A.
Emil A.

Reputation: 3445

var test = window.location.hash.substring(1);

Upvotes: 2

Related Questions