Using pushState to change urls. How do I navigate to the exact placement directly?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Using pushState to change urls. How do I navigate to the exact placement directly?



I am using replaceState (pushState) to change the url as the user scrolls the website.
It is working as expected, and the url is changed. However, if I copy the generated url lets say


example.com/vision



open a new tab and navigate to this, I will get a 404 not found page.
I know that if I use anchor links in the url such as


example.com/#vision



it works, but I dont want the #.


#



How can I overcome this, and navigate the visitor to the exact place in the webpage instead of 404?


function isScrolledIntoView(elem)

var $elem = $(elem);
var $window = $(window);

var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();

var elemTop = $elem.offset().top;
var elemBottom = elemTop + $elem.height();

return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));


$(window).scroll(function()
if (isScrolledIntoView("#1")) window.history.replaceState("state", "title", "home"); return;
if (isScrolledIntoView("#2")) window.history.replaceState("state", "title", "profile"); return;
if (isScrolledIntoView("#3")) window.history.replaceState("state", "title", "vision"); return;
if (isScrolledIntoView("#4")) window.history.replaceState("state", "title", "contacts"); return;
);





Despite changing the URL with pushstate, it still needs to exist when a request is made to it. This is the main reason people use # when dealing with pushState; it means you don't need to deal with problems like this as you only need a single page to deal with all the requests. For it to work in the manner you want you will need to use server-side routing as well as the pushState logic, but then you're getting in to validating that the routes exist from the client - which will be a mess. The short and simple answer here is to just use #
– Rory McCrossan
Aug 8 at 13:50



#


#





@RoryMcCrossan Hi and thank you for your comment. Does the server-side routing is in the terms of rewrite rules such as RewriteRule ^articles/?$ articles/index.php [NC,L] ?
– EnexoOnoma
Aug 8 at 14:04


RewriteRule ^articles/?$ articles/index.php [NC,L]





I'm not familiar with PHP with that level, but it would seem as though that's the right path, yes.
– Rory McCrossan
Aug 8 at 14:06




1 Answer
1



This isn't something you can do client-side. In order for your urls to work and not throw a 404 error, you'll have to rewrite them to the page using htaccess. One option would be to pass the page as a parameter and use it to scroll to the appropriate position using JavaScript.


htaccess


JavaScript



Htaccess:


RewriteRule ^(home|profile|vision|contacts)/?$ /index.php?hash=$1 [NC,L]



PHP:


<script = "application/javascript">
var hash = "<?= $_GET['hash'] ?? ""; ?>";
</script>



JavaScript:


$(function ()
if (hash)
$("html, body").animate(
scrollTop: $("#" + hash).offset().top
, "slow");

);



Then your code should work normally.





γεια σου φίλε, σε ευχαριστώ για την απάντηση. So, what I did, I used the codes above into the website. The one in the htaccess file, and the other into the file and above the code I added in my question. Now, there are no 404 errors, but when I open the page like example.com/profile it does not navigate to the specific anchor...
– EnexoOnoma
Aug 8 at 21:20





I found it, thank you, it works. Αν σε ενδιαφέρει να εργαστείς σε projects ενημέρωσε με. Έχουμε startups σε Ελλάδα και Κύπρο με επενδυτές.
– EnexoOnoma
Aug 8 at 21:43





You're welcome @EnexoOnoma. I'm glad I could help 😄 (Σαν προοπτική σίγουρα θα με ενδιέφερε, ωστόσο, εξαρτάται και από το/τα πρότζεκτ. Επισκέφτηκα το προφίλ σου αλλά δεν έχεις δηλώσει κάποια μέθοδο επικοινωνίας, οπότε σου αφήνω το email μου για να επικοινωνήσεις εσύ).
– Angel Politis
Aug 8 at 22:48





σου έχω απαντήσει μόλις τώρα στο mail
– EnexoOnoma
Aug 9 at 22:38






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard