2

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...

5
  • Syntax looks right...are you sure test contains something? Commented Oct 1, 2013 at 13:35
  • 1
    Are you sure test isn't blank? Try console.log(test) Commented Oct 1, 2013 at 13:35
  • 1
    window.location.hash will begin with a # symbol, if it contains anything at all. You should strip it by adding .substr(1). Commented Oct 1, 2013 at 13:37
  • I logged it just before and just after it isn't blank... Commented Oct 1, 2013 at 13:37
  • thx Blazemonger, that was the problem!! Commented Oct 1, 2013 at 13:40

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.