Page 1 of 1

Internet Explorer: jump to fragment identifier (element ID)

Posted: 20 Aug 2018, 10:51
by jeeswg
- I want to be able to jump to fragment identifiers, e.g. the 'Related' section on AHK help pages.
- If I do something like:
oWB.Navigate("https://autohotkey.com/docs/commands/Run.htm#Related")
That will work the first time. But if I scroll away from that point and execute that line again, it won't jump back.
- I have a workaround below, but I wondered if there was a more direct approach. Thanks.

Code: Select all

;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

;e.g. url
;Run / RunWait - Syntax & Usage | AutoHotkey
;https://autohotkey.com/docs/commands/Run.htm

q:: ;internet explorer - jump to fragment identifier
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
vUrl := RegExReplace(oWB.document.url, "#.*")
if InStr(oWB.document.url, "#Related")
{
	oWB.Navigate(vUrl "#")
	while oWB.busy || !(oWB.ReadyState = 4)
		Sleep, 10
}
oWB.Navigate(vUrl "#Related")
oWB := ""
return

Re: Internet Explorer: jump to fragment identifier (element ID)  Topic is solved

Posted: 20 Aug 2018, 19:24
by swagfag
simulate a click on the heading:

Code: Select all

; ahk v2
#SingleInstance Force

global WB := WBGet("ahk_id" WinExist("ahk_class IEFrame ahk_exe iexplore.exe"))

Esc::ExitApp()
q::goToRelated() {
	try WB.document.parentWindow.frames[0].document.getElementById("Related").children[0].click()
}

WBGet(WinTitle:="ahk_class IEFrame", Svr:=1)
{
	static msg := DllCall("user32\RegisterWindowMessage", Str,"WM_HTML_GETOBJECT", UInt)
	, IID := "{0002DF05-0000-0000-C000-000000000046}" ;IID_IWebBrowserApp
	;, IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ;IID_IHTMLWindow2
	lResult := SendMessage(msg, 0, 0, "Internet Explorer_Server" Svr, WinTitle)
	if (lResult != "")
	{
		VarSetCapacity(GUID,16,0)
		if (DllCall("ole32\CLSIDFromString", WStr,"{332C4425-26CB-11D0-B483-00C04FD90119}", Ptr,&GUID) >= 0)
		{
			DllCall("oleacc\ObjectFromLresult", Ptr,lResult, Ptr,&GUID, UPtr,0, PtrP,pdoc)
			obj := ComObject(9,ComObjQuery(pdoc,IID,IID),1)
			ObjRelease(pdoc)
			return obj
		}
	}
}

Re: Internet Explorer: jump to fragment identifier (element ID)

Posted: 20 Aug 2018, 19:30
by jeeswg
- Oh yeah. That's really great, much obliged.
- It's working very smoothly now. And it doesn't ever jump up to the top of the page like in the last script.
- New script:

Code: Select all

;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

;e.g. url
;Run / RunWait - Syntax & Usage | AutoHotkey
;https://autohotkey.com/docs/commands/Run.htm

q:: ;internet explorer - jump to fragment identifier
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
try oWB.document.parentWindow.frames[0].document.getElementById("Related").children[0].click()
oWB := ""
return
- (I forgot that the new version of the website uses frames, although it looks like in this case no fiddly code for handling frames was required.)
- If anyone has any other ideas, I would still be interested, because it's good to know a range of techniques. Thanks.

Re: Internet Explorer: jump to fragment identifier (element ID)

Posted: 21 Aug 2018, 16:01
by A_AhkUser
The scrollIntoView method can be used to scroll the element on which it's called into the visible area of the browser window - for example:

Code: Select all

#NoEnv
#Singleinstance force
#Warn

GUI, Margin, 0, 0
GUI, Add, ActiveX, w700 h700 vWB, Shell.Explorer
WB.navigate("https://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Accueil_principal")
while (WB.busy or WB.readyState <> 4)
	sleep, 100
WB.document.getElementById("Actualités").scrollIntoView(true) ;  ; if alignToTop parameter is set to false the TOP of the element will be aligned to the TOP of the view
GUI, Show, AutoSize
return

!i::
	WB.document.getElementById("Actualités").scrollIntoView(false) ; if alignToTop parameter is set to false the BOTTOm of the element will be aligned to the BOTTOm of the view
return