# Scroll WebHTMLViewer to top

**URL:** <https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797>\
**Category:** Web\
**Created:** [May 24, 2017, 1:01am UTC](https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797 "2017-05-24T01:01:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Baughman](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@John\_Baughman](https://forum.xojo.com/u/John_Baughman)\
**Post date:** [May 24, 2017, 1:01am UTC](https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797/1 "2017-05-24T01:01:37Z")

</div>

Is there any way to scroll an HTMLViewer to the top programmatically. I would like to do this after resizing the HTMLViewer programmatically. Don’t see anything in the docs that I can use for this purpose.

Thanks,

John

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 24, 2017, 2:35am UTC](https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797/2 "2017-05-24T02:35:21Z")

</div>

[https://www.w3schools.com/jsref/met\_win\_scrollto.asp](https://www.w3schools.com/jsref/met_win_scrollto.asp)

```auto
HTMLViewer1.executeJavaScript("window.scrollTo(0, 0);")
```

---

<div class="post-metadata">

**Author:** ![John\_Baughman](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@John\_Baughman](https://forum.xojo.com/u/John_Baughman)\
**Post date:** [May 24, 2017, 8:29am UTC](https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797/3 "2017-05-24T08:29:25Z")

</div>

Michel B, Thanks!

I can’t seem to get it to work. I tried the following without doing any resizing…

htmlViewer\_Document.executeJavaScript(“window.scrollTo(100,100);”)  
htmlViewer\_Document.executeJavaScript(“window.scrollBy(100, 100);”)

Nothing happens. I have the browser small enought to display the scroll bars

Is there another setting I have to make on the viewer?

John

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 24, 2017, 10:00am UTC](https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797/4 "2017-05-24T10:00:25Z")

</div>

OK. Here is code that gets inside the HTMLViewer content :

```auto
HTMLViewer1.executeJavaScript("document.getElementById('"+HTMLViewer1.controlID+"').getElementsByTagName('iframe')[0].contentWindow.scrollTo(0, 0);")
```

HOWEVER, if the page in the WebHTMLViewer is not on the same domain as your app, it will not work, for security reasons. You will get the following error :

`Could not execute returned javascript: Blocked a frame with origin "http://127.0.0.1:8080" from accessing a cross-origin frame.
Source: document.getElementById('GrxdEByi').getElementsByTagName('iframe')[0].contentWindow.scrollTo(0, 0);`

---

<div class="post-metadata">

**Author:** ![John\_Baughman](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@John\_Baughman](https://forum.xojo.com/u/John_Baughman)\
**Post date:** [May 24, 2017, 7:02pm UTC](https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797/5 "2017-05-24T19:02:51Z")

</div>

Tried your second suggestion. Did not work for me either. No errors.

Not sure I have it coded correctly. The only thing I changed was the viewer name from HMLViewer1 to the name of my viewer htmlViewer\_Document

Maybe I can solve my problem another way. The problem appears to not so much be a position of the vertical scroll bar, but the position of the page within the viewer. After shrinking the size of the viewer by increasing the Top coordinate from 0 to 160, the vertical scroll bar does in fact remain at the 0 position. The content, however, of the viewer, in this case a PDF file, is displayed with the top cut off as if the Top coordinate of the viewer was still at 0. If I move the vertical scroll bar as little as 1 pixel the content will snap down to where it’s supposed to be.

John

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [May 24, 2017, 7:07pm UTC](https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797/6 "2017-05-24T19:07:45Z")

</div>

Well it’s a PDF, it’s not a webpage. The PDF is displayed by the PDF renderer.

If that renderer supports Javascript, then it would work - but I think you might be throwing javascript at something that’s not listening.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 24, 2017, 7:48pm UTC](https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797/7 "2017-05-24T19:48:44Z")

</div>

Indeed, JavaScript is not available for straight PDF.

Another approach would be to place the HTMLViewer, full size, within a WebContainer. Then you can scroll it the way you want with ScrollTo.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:46pm UTC](https://forum.xojo.com/t/scroll-webhtmlviewer-to-top/37797/8 "2020-10-29T18:46:55Z")

</div>


