[Qt-interest] Scroll to anchor in QWebView?
Eric Clark
eclark at ara.com
Thu Jan 28 19:28:51 CET 2010
Thank you Tony! The javascript idea worked. There is one drawback to it though, and that is that I have to make my anchors using the id instead of the standard name attribute. Like so:
<a id="myanchor">
Instead of:
<a name="myanchor">
The standard says that the latter should be used and the anchors will only work with the name attribute. So, to conform to standards, I have to have both the id and the name attribute, like so:
<a name="myanchor" id="myanchor">
This works for what I am trying to do and I appreciate your help!
Eric
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Tony Rietwyk
Sent: Thursday, January 28, 2010 2:13 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] Scroll to anchor in QWebView?
Hi Eric,
You should be able to modify this javascript snippet to achieve what you want. Its quite old and worked in IE and Firefox, but has not been tested in WebKit. This assumes that you can include your own javascript in the page somehow:
function GotoPageAnchor() {
var oElement;
var iOffset;
// When the user has filled in a form, we sometimes want the next page to scroll down,
// but this can't be done from within the application. So it sets "gPageAnchor" to the
// ID of the element to be scrolled to. Then this routine is called as part of PageLoad event.
//
// Doing the scroll directly didn't work, but using a timer did.
//
// Instead of scrolling the element hard against the top of the browser window, a 100 pixel
// margin allows the user to see a bit of the previous context.
if (document.getElementById) {
if (typeof(gPageAnchor) == "string") {
oElement = document.getElementById(gPageAnchor);
if (oElement) {
iOffset = oElement.offsetTop;
while (oElement.offsetParent) {
oElement = oElement.offsetParent;
iOffset += oElement.offsetTop;
}
// alert( "Offset of " + gPageAnchor + " = " + (iOffset) );
if (iOffset > 100) {
iOffset -= 100;
}
setTimeout( "scrollTo(0,"+iOffset+")", 20 );
}
}
}
}
Regards,
Tony
-----Original Message-----
Hello All,
I would like to know if anyone knows of a way to force a QWebView to scroll to an anchor in the web page that it is currently showing?
Thanks,
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100128/d61b65c4/attachment.html
More information about the Qt-interest-old
mailing list