When i had the issue i saved the value into a hidden input:
in html body:
... then put the hidden input on the page and write the value $fid with php echo
then in $(document).ready( function () {
var postId=document.getElementById("fid").value;
so i got my hidden url parameter in php an js.