Page 1 of 1

Getting hash from url in php

Posted: 26 Nov 2019, 18:41
by JanMolendijk
I`m back again found a solution on the question before thanks for that
|My new problem I try to detect # hash with a id

like this url example but it does not skip to the page part ????

//145.53.93.209/Molendijk/home/read.php?id=833#494

I tried two things in my document

Code: Select all

<div id="'.$row["comment_id"].'">
'.$row["comment_id"].'
</div>

Code: Select all

<a name="'.$row["comment_id"].'">#'.$row["comment_id"].'</a>

Getting hash from url in php

Posted: 26 Nov 2019, 20:10
by Admin
You cannot get the #hash part from url in php, because that value isn't send to server.
It can be used in javascript with: window.location.hash

Getting hash from url in php

Posted: 26 Nov 2019, 20:43
by JanMolendijk
could you give any example because i try for 5 hours now without any succes
It works if i put the numbes #123 manual after page load but it not works on page load

Getting hash from url in php

Posted: 27 Nov 2019, 07:06
by Admin
Do you want to put the #hash in javascript or in php?
What did you try?
In php you cannot get the #hash from current page url, unless it is send with javascript via ajax.

In js code you can get the #hash from current page address like this:

Code: Select all

<script>
// For example, url is: //localhost/page.php#123
var url_hsh = window.location.hash;

//testing
alert(url_hsh);  // #123
</script>

Getting hash from url in php

Posted: 27 Nov 2019, 11:43
by JanMolendijk
I tried some options mostly if not working I delete it
but what |I try I cant get after page load skip
to the has-id in php or with a javascript

Ow yes I do not known anything about javascript
if I do something it is lucky-shots

It must be something like this
home/read.php?id=833&title=klant-is-koning#494

I saw also many articles about hash but did not saw any
url with .php?id111#111 or something like

Code: Select all

<script>
window.location.hash = hashTag;
window.location=window.location.href;
</script>

Code: Select all

<script>

$(document).ready(function() {
 var hash = window.location.hash;
 if (hash) {
  var elem = document.getElementById(hash.substring(1));
  if (elem) {
   elem.scrollIntoView();
  }
 }
});

</script>
This script I saw also what working but not on page load with url but only when page is loaded

Code: Select all

<style>

body { 
  font: 14px/2 Georgia, serif; 
}

#page-wrap { 
  max-width: 500px; 
  margin: 1rem auto;
  padding: 1rem;
}

h1, h2 {
  line-height: 1.2;
}

p, ul, h1 { 
  margin: 0 0 1rem 0; 
}

</style>

<script>

// Select all links with hashes
$('a[href*="#"]')
  // Remove links that don't actually link to anything
  .not('[href="#"]')
  .not('[href="#0"]')
  .click(function(event) {
    // On-page links
    if (
      location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') 
      && 
      location.hostname == this.hostname
    ) {
      // Figure out element to scroll to
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      // Does a scroll target exist?
      if (target.length) {
        // Only prevent default if animation is actually gonna happen
        event.preventDefault();
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000, function() {
          // Callback after animation
          // Must change focus!
          var $target = $(target);
          $target.focus();
          if ($target.is(":focus")) { // Checking if the target was focused
            return false;
          } else {
            $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
            $target.focus(); // Set focus again
          };
        });
      }
    }
  });

</script>

<a name="/#'.$row['comment_id'].'" href="#'.$row['comment_id'].'"><button>skip</button></a>
& this script works but also not after page load with url

Code: Select all

<button onclick="myFunction1()">top</button> 

<script>
function myFunction1() {
  location.href = "#top";
}
</script>

<a id="top"></a>

Getting hash from url in php

Posted: 27 Nov 2019, 12:02
by JanMolendijk
I tested it again &nd it seems like to work now `thanks for supporting`