javascript - Insert JS in PHP - on load scroll

923

I'm trying to get my page to scroll to a certain number of pixels down the page when it loads. I'm editing a wordpress template that I created so it's in PHP.

I've got:

<?php
echo "<body onload='window.scroll(0,400)'>";
?>

at the top of the php file but it's not working. what do I need to do to make this work?

455

Answer

Solution:

Use this code in yourheader.php on your child theme or any similar.

<script type="text/javascript">
    $( document ).ready(function() {
        window.scroll(0,400);
    });
</script>

Don't forget insert jquery in your head if this code not working

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
354

Answer

Solution:

The onload event attribute is expecting a function, and you can do that inline withjavascript: in the attribute...

<?php
echo "<body onload='javascript:window.scroll(0,400);'>";
?>

People are also looking for solutions to the problem: Android - Php receive string from android

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.