1

I am trying to get the url of the current page along with the number after #. I am useing this to get the #number: var hash = window.location.hash;. But i want to pass it to a php variable. I used cookies to take the variable from javascript and pass it to php but my webpage remains 2 steps back till echo the value. What other possibilites I have to pass a variable to php. Below is the code i used.

<script>
var hash = window.location.hash;
document.cookie="hash="+hash;
</script>

<?php
$hash='';
if(isset($_COOKIE['hash']))
$hash=$_COOKIE['hash'];
?>

1 Answer 1

1

Try this workaround

<script>
var url = location.href.split('#');  // or location.hash.split("#")
document.cookies = "fragment = " + url[1];
if(url[1] != "<?php echo $_COOKIE['fragment']; ?>"){ // or if(url[1] != ""){
    window.location.reload()
}
</script>

<?php echo $_COOKIE["fragment"]; ?>
Sign up to request clarification or add additional context in comments.

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.