In order to get the page id of fan page, you need to get the signed_request from Facebook.
https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin
In fact, this link talk about the cavas page, but it's the same principle for the fan page.
But if you look carefully the way to get this variable, you can find that,
The signed request is sent via an HTTP POST to the URL set as your Canvas URL in the App Dashboard.
Which means, if you want to get a signed request data, you should get it through a HTTP POST.
To get the page id of fan page is not possible just using javascript. Javascript is a client side language, so you can't access the POST data.
What you need to do is just put your javascript code into a .jsp/.php/...or any other server-side language page. Through the server-side language page, you get the signed request and pass it to your javascript code.
Here is an example in JSP:
<%String signedRequest = request.getParameter("signed_request");%><script>window.signedRequest = "<%=signedRequest%>"</script>
And in your javascript, just decode the string you got and it will contain the page id.
var signedRequest = global.signedRequest;
var data1 = signedRequest.split('.')[1];
data1 = JSON.parse(base64decode(data1));
console.log(data1);
Then you can get data like this:
Object {algorithm: "HMAC-SHA256", expires: 1404925200, issued_at: 1404921078, oauth_token: "CAAUT5x1Ujq8BAPzh2ze1b4QTBkcZAtRCW6zA1xJszUasqoEPp…Fy8fAVEZAyhVaxEaq6ZBw6F4bSFI1s8xtXbBLp7oBFL4wZDZD", page: Object…}
algorithm: "HMAC-SHA256"
expires: 1404925200
issued_at: 1404921078
oauth_token: "CAAUT5x1Ujq8BAPzh2ze1b4QTBkcZAtRCW6zA1xJszUasqoEPpFRfM1ln3x9pb7mLBujyug5iHUifSnyxmPHOLe030wI3H5DYXubnxbPhww9aipSnwoEr6lwctuQaGKxYvDBdZCNuFiaYIduORTWirmZC2rKL86Fy8fAVEZAyhVaxEaq6ZBw6F4bSFI1s8xtXbBLp7oBFL4wZDZD"
page: Object
user: Object
user_id: "1519950734891144"
proto: Object
In the page object, you can find page id.
Object {id: "1522695611287219", liked: true, admin: true}
About how to decode the signed request, you can see this link
https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin
It's the same way.
Hope this can help you.