- The aim was to collect useful methods and properties for window objects and elements. There are currently no event examples because I haven't needed any.
- Please suggest any further additions/provide links. Thanks.
Code: Select all
;JAVASCRIPT EXAMPLES FOR INTERNET EXPLORER
;GET ELEMENT/ELEMENTS
;note: these are often interchangeable:
oElt := oElts[vNum]
oElt := oElts.Item(vNum)
;... as are these:
oElt := oElts.0
oElt := oElts.Item(0)
;get element
oElt := oElt.childNodes.0
oElt := oElt.childNodes[vNum]
oElt := oElt.parentNode
oElt := oElts.0
oElt := oElts[vNum]
oElt := oWB.document.activeElement
oElt := oWB.document.all.0
oElt := oWB.document.all[vNum]
oElt := oWB.document.elementFromPoint(vCurX, vCurY)
oElt := oWB.document.getElementById(vEltID)
oElt := oWB.document.getElementsByClassName(vEltClass).0
oElt := oWB.document.getElementsByClassName(vEltClass)[vNum]
oElt := oWB.document.getElementsByTagName(vEltTag).0
oElt := oWB.document.getElementsByTagName(vEltTag)[vNum]
oElt := oWB.document.querySelector("input") ;tag name is input
oElt := oWB.document.querySelector("[class^=username]") ;class name starts with 'username'
;get elements
oElts := oElt.childNodes
oElts := oWB.document.all
oElts := oWB.document.getElementsByClassName(vEltClass)
oElts := oWB.document.getElementsByTagName(vEltTag)
oElts := oWB.document.querySelectorAll("input") ;tag name is input
oElts := oWB.document.querySelectorAll("[class^=username]") ;class name starts with 'username'
;GET/SET INFORMATION
;get element/elements/document information
MsgBox, % oElt.checked
MsgBox, % oElt.className
MsgBox, % oElt.content
MsgBox, % oElt.getAttribute("href")
MsgBox, % oElt.href
MsgBox, % oElt.id
MsgBox, % oElt.innerText
MsgBox, % oElt.maxlength
MsgBox, % oElt.muted
MsgBox, % oElt.name
MsgBox, % oElt.outerHTML
MsgBox, % oElt.placeholder
MsgBox, % oElt.rel
MsgBox, % oElt.role
MsgBox, % oElt.size
MsgBox, % oElt.tagName
MsgBox, % oElt.title
MsgBox, % oElt.type
MsgBox, % oElt.value
MsgBox, % oElts.length
MsgBox, % oWB.document.title
MsgBox, % oWB.document.url
MsgBox, % oWB.document.documentMode
MsgBox, % oWB.document.hasFocus()
MsgBox, % oWB.document.hidden
;get window information
MsgBox, % oWB.document.parentWindow.history.length ;note: you can get the count but not the individual titles/urls
MsgBox, % oWB.document.parentWindow.navigator.userAgent
MsgBox, % oWB.document.parentWindow.screen.deviceXDPI
MsgBox, % oWB.document.parentWindow.screen.logicalXDPI
MsgBox, % oWB.FullName ;path e.g. 'C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE'
MsgBox, % oWB.LocationName ;info can be lost if clear IE cache so recommended instead: oWB.document.title
MsgBox, % oWB.LocationURL ;info can be lost if clear IE cache so recommended instead: oWB.document.url
MsgBox, % oWB.Name ;e.g. 'Internet Explorer'
MsgBox, % oWB.Type ;e.g. 'HTML Document'
MsgBox, % oWB.Visible
;get zoom %
vDeviceXDPI := oWB.document.parentWindow.screen.deviceXDPI
vLogicalXDPI := oWB.document.parentWindow.screen.logicalXDPI
vZoom := Round(vDeviceXDPI / vLogicalXDPI * 100)
;is webpage busy
MsgBox, % oWB.busy
MsgBox, % oWB.document.readyState
MsgBox, % oWB.readyState ;READYSTATE_COMPLETE := 4
while oWB.busy || oWB.readyState!=4 || oWB.document.readyState!="complete" ;READYSTATE_COMPLETE := 4
Sleep, 10
;get selected text/select text
oWB.document.body.createTextRange().Select()
MsgBox, % oWB.document.getSelection().toString()
MsgBox, % oWB.document.selection.createRange().text
MsgBox, % oWB.document.body.createTextRange().text
;perform an action on an element/window
oElt.click()
oElt.focus()
oElt.scrollIntoView()
oElt.select()
oElt.selectionEnd := StrLen(oElt.value) - 3 ;e.g. Google, set end of selected text in input field
oElt.selectionStart := 3 ;e.g. Google, set start of selected text in input field
oElt.style.width := vEltW "px"
oWB.document.parentWindow.close()
oWB.document.parentWindow.history.go(-1) ;Go Backward one page
oWB.document.parentWindow.history.go(1) ;Go Forward one page
oWB.document.parentWindow.scrollBy(vX, vY)
oWB.document.parentWindow.scrollTo(vX, vY)
oWB.document.write(vHtml)
oWB.ExecWB(4, 0) ;OLECMDID_SAVEAS := 4
oWB.ExecWB(12, 0) ;OLECMDID_COPY := 12
oWB.ExecWB(17, 0) ;OLECMDID_SELECTALL := 17
oWB.ExecWB(19, 2, vTextSize, 0) ;OLECMDID_ZOOM := 19 ;(text size 0/1/2/3/4)
oWB.ExecWB(32, 0) ;OLECMDID_FIND := 32
oWB.ExecWB(63, 2, vZoom, 0) ;OLECMDID_OPTICAL_ZOOM := 63 ;(zoom % e.g. 100, 135)
oWB.GoBack()
oWB.GoForward()
oWB.Navigate(vUrl)
oWB.Quit ;close tab
oWB.Quit() ;close tab
oWB.Refresh()
oWB.Stop()
oWB.Visible := -1 ;true
oWB.Visible := 0 ;false
;get element/window dimensions
;note: some use width/height, some use right/bottom
oElt := oWB.document.documentElement
;oElt := oWB.document.body
;oElt := oWB.document.body.parentElement
MsgBox, % oElt.clientLeft " " oElt.clientTop " " oElt.clientWidth " " oElt.clientHeight
MsgBox, % oElt.offsetLeft " " oElt.offsetTop " " oElt.offsetWidth " " oElt.offsetHeight
MsgBox, % oElt.scrollLeft " " oElt.scrollTop " " oElt.scrollWidth " " oElt.scrollHeight
oRect := oElt.getBoundingClientRect()
MsgBox, % oRect.left " " oRect.top " " oRect.right " " oRect.bottom
MsgBox, % oWB.document.parentWindow.screen.width " " oWB.document.parentWindow.screen.height
MsgBox, % oWB.left " " oWB.top " " oWB.width " " oWB.height
;get style information
try vInfo := oElt.currentStyle.backgroundImage
try vInfo := oElt.currentStyle.height
try vInfo := oElt.style.backgroundImage
try vInfo := oElt.style.height
;get/set a custom property (e.g. you can use this to give unique identifiers to IE objects)
;e.g. I get the hCtl (hWnd) for each Internet Explorer_Server control
;... and get the object for each control using WBGet
;... and then add a property to each object specifying the hCtl
oWB.PutProperty("MyProperty", 1234)
oWB.PutProperty("MyProperty", hCtl)
MsgBox, % oWB.GetProperty("MyProperty")
;get hWnd
MsgBox, % oWB.application.hWnd
MsgBox, % oWB.HWND
MsgBox, % oWB.parent.hWnd
;check for element existence
if IsObject(oElt)
if oElts.length
;objects
;note: oHTML is equivalent to oWB.document
oHTML := ComObjCreate("HTMLFile")
oWB := ComObjCreate("InternetExplorer.Application")
for oWB in ComObjCreate("Shell.Application").Windows
oWB := WBGet("ahk_id " hWnd) ;note: WBGet is a custom function
;get location information
;e.g. url:
;Variables and Expressions - Definition & Usage | AutoHotkey
;https://autohotkey.com/docs/Variables.htm#BuiltIn
MsgBox, % oWB.document.location.hash ;#BuiltIn
MsgBox, % oWB.document.location.host ;autohotkey.com
MsgBox, % oWB.document.location.hostname ;autohotkey.com
MsgBox, % oWB.document.location.href ;https://autohotkey.com/docs/Variables.htm#BuiltIn
MsgBox, % oWB.document.location.origin ;https://autohotkey.com
MsgBox, % oWB.document.location.pathname ;/docs/Variables.htm
MsgBox, % oWB.document.location.port ;(blank)
MsgBox, % oWB.document.location.protocol ;https:
MsgBox, % oWB.document.location.search ;(blank)
;COMOBJTYPE FUNCTION
;get info from AutoHotkey's ComObjType function
MsgBox, % ComObjType(ComObject) ;VarType
MsgBox, % ComObjType(ComObject, "Name") ;IName
MsgBox, % ComObjType(ComObject, "IID")
MsgBox, % ComObjType(ComObject, "Class") ;CName
MsgBox, % ComObjType(ComObject, "CLSID")
;==================================================
;FRAMES
;e.g. url:
;Variables and Expressions - Definition & Usage | AutoHotkey
;https://autohotkey.com/docs/Variables.htm
MsgBox, % oWB.document.documentElement.innerText
MsgBox, % oWB.document.frames.0.document.documentElement.innerText
;frames example:
try MsgBox, % oWB.document.frames.length
catch
MsgBox, 0
oFrames := oWB.document.frames
Loop, oFrames.length
MsgBox, % oFrames[A_Index-1].name
;==================================================
;TABLES AND X-UA-COMPATIBLE
q:: ;example demonstrating 'X-UA-Compatible' and tables/rows/cells
vUrl := "https://hotkeyit.github.io/v2/docs/commands/WinApi.htm"
vPath := A_Desktop "\WinApi [AutoHotkey_H].htm"
;UrlDownloadToFile, % vUrl, % vPath
oHTML := ComObjCreate("HTMLFile")
;note: 'X-UA-Compatible' not needed as the htm file already uses it
;vMeta := "<meta http-equiv='X-UA-Compatible' content='IE=9'>"
;vMeta := "<meta http-equiv='X-UA-Compatible' content='IE=11'>"
;vMeta := "<meta http-equiv='X-UA-Compatible' content='IE=edge'>"
;oHTML.write(vMeta)
vHtml := FileRead(vPath)
oHTML.write(vHtml)
;get version number + test getElementsByClassName to confirm that 'X-UA-Compatible' was used successfully
;MsgBox, % oHTML.documentMode
;MsgBox, % oHTML.getElementsByClassName("Info").length
;MsgBox, % oHTML.getElementsByClassName("Syntax").length
oTables := oHTML.getElementsByTagName("tbody")
oTable := oTables.0
oRows := oTable.rows
oRow := oRows.0
oCells := oRow.cells
MsgBox, % oTables.length " " oRows.length " " oCells.length
MsgBox, % oCells.0.innerText "`r`n" oCells.1.innerText
return
WBGet function - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=39869
jeeswg's Internet Explorer and HTML tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31766
Links (MSDN and w3schools):
InternetExplorer object (Windows)
https://msdn.microsoft.com/en-us/windows/desktop/aa752084
Screen Object
https://www.w3schools.com/jsref/obj_screen.asp
Location Object
https://www.w3schools.com/jsref/obj_location.asp
HTML DOM Style object
https://www.w3schools.com/jsref/dom_obj_style.asp
Links (tables):
HTML Tables
https://www.w3schools.com/html/html_tables.asp
HTML DOM Table Object
https://www.w3schools.com/jsref/dom_obj_table.asp
HTML DOM Table rows Collection
https://www.w3schools.com/jsref/coll_table_rows.asp
HTML DOM Table cells Collection
https://www.w3schools.com/jsref/coll_table_cells.asp
Links (querySelector/querySelectorAll):
CSS Selectors Reference
https://www.w3schools.com/cssref/css_selectors.asp
css3 - CSS Selector Clarification: |= vs ^= - Stack Overflow
https://stackoverflow.com/questions/35370441/css-selector-clarification-vs
querySelector/querySelectorAll examples - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=62113
[has a QUERYSELECTOR / QUERYSELECTORALL section]
jeeswg's Internet Explorer and HTML tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31766
Links (IE object to control hWnd):
[get hWnd for TabWindowClass/Internet Explorer_Server control associated with an IE object]
Cast COM Object IWebBrowser2 to IServiceProvider - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=67163
[EDIT:] added: get zoom %, .content, .rel, style object link, querySelector links, .role, .title, control hWnd link, .currentStyle