UDF > IE >


_IEHeadInsertEventScript

Insère un script Java dans l'entête du document

#include <IE.au3>
_IEHeadInsertEventScript ( ByRef $oObject, $sHTMLFor, $sEvent, $sScript )

Paramètres

$oObject Variable objet InternetExplorer.Application, Window ou Frame
$sHTMLFor L'élément HTML pour la surveillance des événements (par exemple, "document", "window" ou un ID d'élément)
$sEvent L'événement à surveiller (par exemple "onclick" ou "oncontextmenu")
$sScript Chaîne Javascript qui doit être exécutée

Valeur de retour

Succès: Retourne 1.
Échec: Retourne 0 et définit @error <> 0.
@error: 2 ($_IEStatus_COMError) - Erreur COM dans la référence d'un objet
3 ($_IEStatus_InvalidDataType) - Type de donnée invalide
@extended: Contient le nombre de paramètres invalides

Remarques

Lors de l'utilisation de ObjEvent() , AutoIt est capable d'être informé des événements via COM, mais il les gère de manière asynchrone (plutôt que synchrone comme ils sont traités dans le cadre du navigateur).
Cette routine vous permet d'injecter du code qui est géré dans le cadre du navigateur.

Notez que les éléments qui n'ont pas un ID affecté peuvent encore être utilisés en obtenant leurs propriétés "uniqueID" avec _IEPropertyGet().

En relation

_IEDocInsertHTML, _IEDocInsertText, _IEPropertyGet

Exemple

Exemple 1

; Ouvre une instance du navigateur avec la page  exemple de base, insère un
; événement script dans l'entête du document qui crée
; une alerte JavaScript quand quelqu'un clique sur le document

#include <IE.au3>

Local $oIE = _IE_Example("basic")
_IEHeadInsertEventScript($oIE, "document", "onclick", "alert('Someone clicked the document!'); ")

Exemple 2

; Open a browser with the basic example page, insert an
; event script into the head of the document that creates
; a JavaScript alert when someone tries to right-click on the
; document and then the event script returns "false" to prevent
; the right-click context menu from appearing

#include <IE.au3>

Local $oIE = _IE_Example("basic")
_IEHeadInsertEventScript($oIE, "document", "oncontextmenu", "alert('No Context Menu'); return false")

Exemple 3

; Open a browser with the basic example page, insert an
; event script into the head of the document that creates a
; JavaScript alert when we are about to navigate away from the
; page and presents the option to cancel the operation.

#include <IE.au3>

Local $oIE = _IE_Example("basic")
_IEHeadInsertEventScript($oIE, "window", "onbeforeunload", _
        "alert('Example warning follows...'); return 'Pending changes may be lost'; ")
_IENavigate($oIE, "www.autoitscript.com")

Exemple 4

; Open a browser with the basic example page, insert an
; event script into the head of the document that prevents
; selection of text in the document

#include <IE.au3>

Local $oIE = _IE_Example()
_IEHeadInsertEventScript($oIE, "document", "ondrag", "return false; ")
_IEHeadInsertEventScript($oIE, "document", "onselectstart", "return false; ")

Exemple 5

; Open a browser with the AutoIt homepage, insert an
; event script into the head of the document that prevents
; navigation when any link is clicked and log the URL of the
; clicked link to the console

#include <IE.au3>

Local $oIE = _IECreate("http://www.autoitscript.com")

Local $oLinks = _IELinkGetCollection($oIE)
For $oLink In $oLinks
    Local $sLinkId = _IEPropertyGet($oLink, "uniqueid")
    If @error Then Exit
    _IEHeadInsertEventScript($oIE, $sLinkId, "onclick", "return false; ")
    If @error Then Exit
    ObjEvent($oLink, "_Evt_")
Next

; idle as long as the browser window exists
While WinExists(_IEPropertyGet($oIE, "hwnd"))
    Sleep(100)
WEnd

Func _Evt_onClick()
    Local $o_Link = @COM_EventObj
    ConsoleWrite($o_Link.href & @CRLF)
EndFunc   ;==>_Evt_onClick