-2

The default behaviour of a web page is that the browser displays it "from the top". I can request it to be displayed from a specific bookmark if I set for example the id="my_bookmark" attribute to an element in the page and then request the URL mywebsite.com/mypage.html#my_bookmark.

I feel like there has to be a way to change this behaviour so that a page will automatically load at a specified bookmark further down the page, because that seems to me like a no-brainer, yet I can't find anything on that. Maybe I'm looking in the wrong places ?

Specific questions :

  1. Is there a simple HTML5 way to do this ? Like some sort of <meta> tag or other that tells the browser at what bookmark to load a page by default ?
  2. Failing that, I'm curious : how are bookmarks handled at all ? I feel like if they're included in the URL it has to be because the server does something with that information; can I do anything about the bookmarks with PHP ? Or even with the damned .htaccess file ?
  3. If this feature doesn't exist anywhere, is there a good reason that I just don't see ? It does seem to me like it's a very straightforward thing.

If you're wondering, my use case is this : I'm making a web-based game that has the player move through a story by solving puzzles. Every so often the player discovers a new "bit" of the story (a couple of new paragraphs). When they do, they are taken to story.php where all the pieces of the story that they have so far discovered will be displayed, in reading order, from top to bottom. But I want the player to land on story.php directly at the last bit of the story that they discovered, and I want them to scroll up if they want to read the previous stuff. I feel like that has to be doable, and fairly simple.

The stuff I know : Some HTML5, some CSS, some PHP. I don't really want to use a specific new PHP library if it means I have to install it, and I feel like I want to avoid JavaScript, but maybe you can talk me into it.

  • This must be done with JS. PHP doesn't know anything about the rendering window. – user3783243 May 16 '20 at 02:12
  • 1
    "I feel like that has to be doable, and fairly simple." Yeah, by including a fragment in the URL, as described by yourself in the very first paragraph ([the part after the # character in a URL is called fragment](https://tools.ietf.org/html/rfc3986#section-3). Bookmarks are something else entirely). What's wrong with that? – Peter May 16 '20 at 06:58

1 Answers1

2

If you already have the proper anchor tags in place, just point your links at story.php#anchor.

There's no way of identifiying these "Fragments" in PHP or telling the browser to scroll to a specific position from PHP, though. The Fragments are never sent to the webserver and only used internally by the Browser. And the position of the scrollbar is controlled by the browser based on the size of the content corresponding to the size of the Browser Window and screen resolution, neither of which is known to the server-side PHP code.

You can use the window.scrollTo function to scroll to a specific coordinate in your document. (See: https://developer.mozilla.org/de/docs/Web/API/Window/scrollTo)

Mastacheata
  • 1,866
  • 2
  • 21
  • 32