Alex Groves
Alex Groves

Reputation: 37

JavaScript- won't scroll to bottom of page?

This code is trying to scroll to the bottom of the page. But for some reason it won't work (Link has a ?wall=1 inside)

<!--load to bottom if posting on wall-->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script>
 var wall=<?php echo $_GET['wall'];?>;
 if (wall===1) {
   window.scroll(0,document.body.scrollHeight)
 }
</script>

Please help as soon as possible!

Upvotes: 0

Views: 120

Answers (2)

Niklesh Raut
Niklesh Raut

Reputation: 34914

Use quotes to use php code inside php code .Also == is use for equality of any type and === is eqality of same type, So use according to it.

    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <html>
    some text
    </html>
    <script type="text/javascript">
     var wall = "<?php echo $wall = isset($_GET['wall']) ? $_GET['wall'] : '';  ?>";
     if (wall === "1") { //use == for different type and === for same type.
       window.scroll(0,document.body.scrollHeight);
     }
    </script>

Upvotes: 1

Poul Kruijt
Poul Kruijt

Reputation: 71891

window.scroll() is not available in browsers yet read: https://developer.mozilla.org/en-US/docs/Web/API/Window/scroll

Use window.scrollTo()

You should also wait till the document is ready with loading and additionally also until angular is done with its template processing, because this changes the document.body.scrollHeight value.

Additionally, if there is no GET variable you might run into an error as well

Consider changing it to this

var wall="<?php echo isset($_GET['wall']) ? $_GET['wall'] : 0;?>";

Or if you use PHP7:

var wall="<?=$_GET['wall'] ?? 0;?>";//also added quotes

Upvotes: 0

Related Questions