mercredi 9 septembre 2020

How to prevent scroll move when location.href value set?

I have a web page which has a image and a title in anchor tag. There was a request to prevent scroll to top of a web page when clicking a anchor tag before url changing. I attempted it various way(e.preventDefault etc..) but browser occurs scroll moving before page changing. Please give me advice if there are solutions.

very appereciate for reading.

ex)

<html>
<body>
    <a id='clickMe'>Click!</a>
</body>
</html>

<script>
    $("#clickMe").click(function(e) {
        e.preventDefault();
        location.href = 'http://www.google.com';
        // It occurs scroll to top before page changing to google.
    });
</script>



Aucun commentaire:

Enregistrer un commentaire