简体   繁体   中英

navigate to php page and scroll

My boss asked something very specific: a page.php that will take one argument, and display text based on that argument + when the page is displayed scroll to an anchor on the page based on that argument.

How is that possible. If I use argument ?a=1 the php can handle it, but how can the browser/javascript? If I use anchor #a1 the browser can handle it, but I was unable to read this with php. I used:

$_SERVER["REQUEST_URI"]

Any creative solutions to this problem?

This is one solution for it. If it could not be useful, tell me to give another solution to you.

  1. Read the value of passed item to the URL using $_GET[""]
  2. Process its value and do what you need.
  3. Use the code below to navigate the current position to your pre-defined anchor

<?php
if (isset($_GET['a'])) {
   $a = $_GET['a'];
   $anchor = '#'.$a;
   echo('
   <script type="javascript">window.location.href="'.$anchor.'"</script>
   ');
}
?>

Don't forget that you can combine a server-side programming language with a client-side one. I hope you can solve this issue. Otherwise, comment this post.

window.location.search

The window.location object contains what you need. cf. this: http://davidwalsh.name/javascript-window-location

The Hash is not seen by PHP, because the browsers don't send it to the server. They are normally used the jump to a specific location in the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM