Internet Explorer: is element the active element

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 5939
Joined: 19 Dec 2016, 01:58
Location: UK

Internet Explorer: is element the active element

25 Sep 2017, 19:06

From samples on the Internet, I might have thought that something like this might work. That you could compare elements in getElementsByTagName against activeElement, to check if they were the same element. Can this be done in AutoHotkey in this way, or in a similar way? Thanks for reading.

Code: Select all

;[WBGet function]
;Basic Webpage Controls with JavaScript / COM - Tutorial - Tutorials - AutoHotkey Community
;https://autohotkey.com/board/topic/47052-basic-webpage-controls-with-javascript-com-tutorial/

q:: ;internet explorer - is element the active element
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
oElt := oWB.document.activeElement
vEltTag := oElt.tagName
;MsgBox, % oWB.document.getElementsByTagName(vEltTag).length
Loop, % oWB.document.getElementsByTagName(vEltTag).length
{
	oElt2 := oWB.document.getElementsByTagName(vEltTag).item[A_Index-1]

	;neither of these work:
	if (oElt = oElt2)
		MsgBox, % A_Index
	if (oWB.document.activeElement = oWB.document.getElementsByTagName(vEltTag).item[A_Index-1])
		MsgBox, % A_Index
}
oWB := ""
MsgBox, % "done"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
kczx3
Posts: 761
Joined: 06 Oct 2015, 21:39

Re: Internet Explorer: is element the active element

25 Sep 2017, 19:37

I don’t believe that the DOM keeps a unique reference to each and every element within it. The element would need an “id” attribute to do that. Also, you don’t need to use the .item() method. Just reference the array position immediately after .getElementsByTagName()
User avatar
jeeswg
Posts: 5939
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: is element the active element

03 Oct 2017, 21:38

@kczx3: Thanks a lot for the tip re. '.item'. I was wondering about how round brackets were possible, and clearly, it's because 'Item' is a method.

Btw one thing related to this, which would be useful, is to start with an element, and work out that it's the nth child of its parent, and do this until it reaches the earliest ancestor element, and by doing this, this would help to work the code needed to refer to the element.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 5939
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: is element the active element

27 Dec 2018, 16:28

- Here's an improved (optimised) version of the script above that doesn't work.

Code: Select all

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

q:: ;internet explorer - is element the active element
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
oElt := oWB.document.activeElement
vEltTag := oElt.tagName
oElts := oWB.document.getElementsByTagName(vEltTag)
;MsgBox, % oElts.length
Loop, % oElts.length
{
	oElt2 := oElts[A_Index-1]

	;this doesn't work:
	if (oElt = oElt2)
		MsgBox, % A_Index
}
oWB := oElts := oElt2 := oElt := ""
MsgBox, % "done"
return
- It's curious that oElts[vNum], oElts.item(vNum) and oElts.item[vNum] all work. Why is that?
- By contrast:

Code: Select all

q:: ;explorer - get the first file in the folder
WinGet, hWnd, ID, A
oWin := ""
for oWin2 in ComObjCreate("Shell.Application").Windows
	if (oWin2.HWND = hWnd)
	{
		oWin := oWin2
		break
	}
oWin2 := ""
if !oWin
	return

MsgBox, % oWin.Document.Folder.Items.Item(0).path
MsgBox, % oWin.Document.Folder.Items.Item[0].path
;MsgBox, % oWin.Document.Folder.Items.0.path ;didn't work

vNum := 0
MsgBox, % oWin.Document.Folder.Items.Item(vNum).path
MsgBox, % oWin.Document.Folder.Items.Item[vNum].path
;MsgBox, % oWin.Document.Folder.Items[vNum].path ;didn't work

oWin := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: asad41163, AviationGuy, Bing [Bot], Google [Bot], ktbjx and 168 guests