Internet Explorer: JavaScript examples

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 5956
Joined: 19 Dec 2016, 01:58
Location: UK

Internet Explorer: JavaScript examples

27 Dec 2018, 15:42

- I've collected some classic JavaScript examples for use with Internet Explorer.
- 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")

;get elements
oElts := oElt.childNodes
oElts := oWB.document.all
oElts := oWB.document.getElementsByClassName(vEltClass)
oElts := oWB.document.getElementsByTagName(vEltTag)
oElts := oWB.document.querySelectorAll("input")

;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/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

;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
Links (AutoHotkey forum):
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
querySelector/querySelectorAll examples - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=62113

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

[EDIT:] added: get zoom %, .content, .rel, style object link, querySelector link, .role, .title
Last edited by jeeswg on 17 Feb 2019, 20:25, edited 6 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

Re: Internet Explorer: JavaScript examples

29 Dec 2018, 10:11

That's a hell of cheat sheet. Thanks dude.
live ? long & prosper : regards
A_AhkUser
Posts: 978
Joined: 06 Mar 2017, 16:18
Location: France

Re: Internet Explorer: JavaScript examples

29 Dec 2018, 13:37

nice :thumbup:
burque505
Posts: 868
Joined: 22 Jan 2017, 19:37

Re: Internet Explorer: JavaScript examples

29 Dec 2018, 15:14

Thanks, jeeswg!
User avatar
SKAN
Posts: 362
Joined: 29 Sep 2013, 16:58

Re: Internet Explorer: JavaScript examples

30 Dec 2018, 06:36

Thank you :)
rommmcek
Posts: 508
Joined: 15 Aug 2014, 15:18

Re: Internet Explorer: JavaScript examples

30 Dec 2018, 06:52

Great vade mecum!
Q: Why the heck oWB.document.parentwindow.devicePixelRatio doesn't work?
elmo
Posts: 92
Joined: 09 Oct 2013, 09:08

Re: Internet Explorer: JavaScript examples

11 Jan 2019, 21:46

@jeeswg

This collection seems really valuable. Thank you for posting.

May I trouble you for guidance on elementFromPoint?

Code: Select all

oWB := WBGet()
MsgBox, % oWB.document.url ; this works
MsgBox, % oWB.document.elementFromPoint( 953 , 453 ) ; no result
Am I using it correctly?

Also, where does "oElts" come from? I suppose it is an object but can not figure out the origin.
User avatar
jeeswg
Posts: 5956
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: JavaScript examples

11 Jan 2019, 22:33

- Thanks for all the responses, all from users that I really appreciate, making it all the more meaningful. (@A_AhkUser: getAttribute is thanks to you, and you've helped me a lot with JavaScript queries!) (@SKAN: Thanks for your functions!) (@rommmcek: MIDI master!) (Thanks burque505, CyL0N and elmo!)

- @elmo: You are doing the equivalent of this:

Code: Select all

oElt := oWB.document.elementFromPoint( 953 , 453 )
MsgBox, % oElt
- You are applying MsgBox to an object, what do you expect to see? Normally in AutoHotkey, if you apply MsgBox to an object, you see nothing. However, in AHK v2, applying MsgBox to an object will show a string, if the object has a ToString method defined.
- Here is some further test code. If no element is found, IsObject shows 0.

Code: Select all

q:: ;test elementFromPoint
oWB := WBGet()
MsgBox, % oWB.document.url
oElt := oWB.document.elementFromPoint( 953 , 453 )
;oElt := oWB.document.elementFromPoint( 100 , 100 )
MsgBox, % IsObject(oElt)
try MsgBox, % oElt.tagName
try MsgBox, % oElt.className
try MsgBox, % oElt.innerHTML
try MsgBox, % oElt.outerText
return
- oElt, oElts and oWB are arbitrary variable names that I chose.
- oElts is intended to represent a collection of elements, you can apply .length for example to get the element count. See 'get elements' near the top of the OP for examples of where collections are stored as oElts .
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
elmo
Posts: 92
Joined: 09 Oct 2013, 09:08

Re: Internet Explorer: JavaScript examples

11 Jan 2019, 23:37

@jeeswg

So appreciate the timely response. Yes, I am using 1.1. Excited to try this at the office tomorrow. Will report back.
elmo
Posts: 92
Joined: 09 Oct 2013, 09:08

Re: Internet Explorer: JavaScript examples

12 Jan 2019, 13:39

@jeeswg

Everything that you suggested works. Thank you so much.

I should have also stated that the objective is to click the returned object, however

Code: Select all

oElt.click()
does nothing.

Any thoughts on what I am doing wrong?
User avatar
jeeswg
Posts: 5956
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: JavaScript examples

12 Jan 2019, 15:27

- That's great.
- You could try clicking on the child nodes or parent/ancestor nodes. E.g.

Code: Select all

oElts := oElt.childNodes
Loop, % oElts.length
	oElts[A_Index-1].click()

oElt.parentNode.click()
oElt.parentNode.parentNode.click()
oElt.parentNode.parentNode.parentNode.click()
- It may be that click doesn't work, perhaps there is another way to invoke the element. Perhaps you could use .focus() and ControlSend {Enter} or SendInput {Enter}.
- If none of those work you could start a new thread in Ask For Help. Possibly you could do something more advanced using events.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
elmo
Posts: 92
Joined: 09 Oct 2013, 09:08

Re: Internet Explorer: JavaScript examples

12 Jan 2019, 15:37

@jeeswg

Copy that and appreciate the options.

Hmmm ... Question ... in

Code: Select all

oElt := oWB.document.elementFromPoint(vCurX, vCurY)
Are vCurX and vCurY windows screen coordinates or IE browser window coordinates?
burque505
Posts: 868
Joined: 22 Jan 2017, 19:37

Re: Internet Explorer: JavaScript examples

12 Jan 2019, 17:08

@rommmcek, @jeeswg, am I actually getting the true devicepixelratio here?
Spoiler
Thanks in advance,
burque505
elmo
Posts: 92
Joined: 09 Oct 2013, 09:08

Re: Internet Explorer: JavaScript examples

12 Jan 2019, 18:44

@jeeswg

Ah, ok, browser window coordinates.

Hmmm ... Are you aware of an efficient way to find the top left corner of the WBGet that is returned so that the screen and window coordinates can be synchronized?

Again, thank you so much for the most excellent help.
User avatar
jeeswg
Posts: 5956
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: JavaScript examples

12 Jan 2019, 19:59

- @rommmcek and @burque505: I've never really looked at devicePixelRatio.
- The only thing I've been interested in re. zooms etc was to get the Internet Explorer 'View, Zoom' value, which I did like so:

Code: Select all

;Internet Explorer get/set zoom/text size - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=26359

vLogicalXDPI := oWB.document.parentWindow.screen.logicalXDPI
vDeviceXDPI := oWB.document.parentWindow.screen.deviceXDPI
vZoom := Round(vDeviceXDPI / vLogicalXDPI * 100)
- One thing that might be useful (for checking or for the eventual code) is ControlGetPos applied to an Internet Explorer_Server control. (Or ControlGet-Hwnd and WinGetPos applied to the hWnd of the Internet Explorer_Server control.)
- I did a search for "Internet Explorer" devicePixelRatio and found some links implying it's a fiddly question:
javascript - window.devicePixelRatio does not work in IE 10 Mobile? - Stack Overflow
https://stackoverflow.com/questions/163 ... -10-mobile
cross browser - IE9 devicePixelRatio equivalent - Stack Overflow
https://stackoverflow.com/questions/882 ... equivalent
javascript - window.devicePixelRatio browser support - Stack Overflow
https://stackoverflow.com/questions/163 ... er-support
- And here's a documentation page:
Window.devicePixelRatio | MDN
https://developer.mozilla.org/en-US/doc ... PixelRatio

- @elmo: The documentation mentions coordinates as relative the viewport.
DocumentOrShadowRoot.elementFromPoint() | MDN
https://developer.mozilla.org/en-US/doc ... tFromPoint
- I have some key code re. elementFromPoint() here:
Internet Explorer get element under cursor (show borders, show text) (any zoom percentage) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=29458
- I've been intending to come back to it, to see if I can finally get a working script to handle getting the element under the cursor for any number of frames. (It's been on my radar this year, especially since I completed some fiddly Acc code recently.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
burque505
Posts: 868
Joined: 22 Jan 2017, 19:37

Re: Internet Explorer: JavaScript examples

12 Jan 2019, 20:20

@jeeswg, thanks a lot. I have actually seen most of those links for devicepixelratio already. I just saw this post and came away knowing less than when I started :(
It seems to me my code is returning the zoom value, if not the devicepixelratio, and I'll be hornswoggled if I can tell the difference.
I tried your code, and it always comes back with the same number regardless of the zoom value I've picked (in other words, for me at least the logicalXDPI and the deviceXDPI are always the same number, regardless of my browser's zoom setting).
Regards,
burque505
rommmcek
Posts: 508
Joined: 15 Aug 2014, 15:18

Re: Internet Explorer: JavaScript examples

14 Jan 2019, 16:14

Didn't notice this thread continues. @burque505: nice find!
I think you found ratio between Win zoom and Html zoom!

Code: Select all

dpr(doc) {
	xdpi := doc.parentwindow.screen.deviceXDPI
	devicepixelratio := A_ScreenDPI / xdpi ; -> (Win Zoom / Html Zoom)
	return devicepixelratio
}

;Win Dpi    Win Zoom
;96      -> 100%
;120     -> 125%
;144     -> 150%
;...
P.s.:
burque505 wrote:I tried your code, and it always comes back with the same number regardless of the zoom value I've picked (in other words, for me at least the logicalXDPI and the deviceXDPI are always the same number, regardless of my browser's zoom setting).
This cannot be true! You lately praised iWBLearner doing fine with different IE zoom values, using the same jeeswig's code!
rommmcek
Posts: 508
Joined: 15 Aug 2014, 15:18

Re: Internet Explorer: JavaScript examples

14 Jan 2019, 16:33

So to get IE Zoom you need only: IEZoom := round(100*xdpi/96)
P.s.: Together we know more!
burque505
Posts: 868
Joined: 22 Jan 2017, 19:37

Re: Internet Explorer: JavaScript examples

14 Jan 2019, 16:35

:facepalm: :headwall: @rommmcek, IWBLearner does in fact do fine, and the error was in MY code, not jeeswg's.
And my error was this: I had both values set to "deviceXDPI" instead of "deviceXDPI" for one and "logicalXDPI" for the other.
rommmcek
Posts: 508
Joined: 15 Aug 2014, 15:18

Re: Internet Explorer: JavaScript examples

20 Jan 2019, 15:12

I missed your answer again.
burque505 wrote:I had both values set to "deviceXDPI" instead of "deviceXDPI" for one and "logicalXDPI" for the other.
My current standpoint is: For AutoHotkey (running under Windows OS) we don't need logicalXDPI at all. It will always yield 96 (see above). Not so in other OSes (for Mac I believe 75, but not supporting Ahk), etc.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: afe, Tigerlily and 28 guests