Jump to content

how to prevent/control refresh, navigation or close of a webpage


DaleHohm
 Share

Recommended Posts

Under the right circumstances, you may find this very useful...

Note that this can be used on any webpage, whether you (_IE)Create it or (_IE)Attach to it on the Internet.

; Example demonstrating how to prevent/control refresh, navigation or close of a webpage.
; Particularly useful when you are writing an application with extensive Dynamic HTML
; that can be wiped out with a simple page refresh.

#include <IE.au3>

$oIE = _IECreate()

; Add HTML to blank page
$oBody = _IETagnameGetCollection($oIE, "body", 0)
_IEDocInsertHTML($oBody, "<a href=http://www.autoitscript.com>Navigate to http://www.autoitscript.com</a><p>")
_IEDocInsertHTML($oBody, "<BUTTON id=b1 value='Normal Exit'>Normal Exit</button>&nbsp;")
_IEDocInsertHTML($oBody, "<BUTTON id=b2 value='Safe Exit'> Safe Exit </button>")

; Get references to buttons and setup event handlers
$oButton1 = _IEGetObjById($oIE, "b1")
$oButton2 = _IEGetObjById($oIE, "b2")
$oEvtButton1 = ObjEvent($oButton1, "evtButton_")
$oEvtButton2 = ObjEvent($oButton2, "evtButton_")

; Setup random alert string to appear in popup, add a function to be called when onbeforeunload is triggered.
; This is the "magic" in this example
$sRandom = "MY-ALERT-" & Random(1000000, 9999999, 1) 
IEHeadInsertScript($oIE, "function promptBeforeUnload() { return '" & $sRandom & "'; }")
IEEval($oIE, "window.onbeforeunload = promptBeforeUnload;")

; With this Javascript injected in the page, any attempt to navigate, refresh or close will
; result in a popup like the following.  The key is adding text to the Return statement of the
; function that is bound to the onbeforeunload event.
;
; The TrapAlert routine called in the idle loop watches for the unique string in the alert ($sRandom),
; quickly covers the alert with a splash screen and presses Cancel to abort the action.
;
; This is not a foolproof method if someone is trying to break out because there is often
; enough of a lag for the user to press OK, but in the right circumstances is very effective.

; ---------------------------------------------------------------
; Are you sure you want to navigate away from this page?
;
; MY-ALERT-2522442
;
; Press OK to continue, or Cancel to stay on the current page.
;
;                                             OK       Cancel
; ---------------------------------------------------------------

While __IEIsObjType($oIE, "browser")
    TrapAlert()
    Sleep(200)
WEnd

; --------------- Functions ---------------
Func TrapAlert()
    If WinExists("", $sRandom) Then
        ControlClick("", $sRandom, "Button2")
        SplashTextOn("Navigation not allowed!", _
            "Use the controls supplied or you will lose all of your work!", -1, -1, -1, -1, 33)
        Sleep(5000)
        SplashOff()
    EndIf
EndFunc

Func evtButton_onclick()
    Local $o_object = @COM_EventObj
    Switch $o_object.value
        Case "Normal Exit"
            $oIE.quit
        Case "Safe Exit"
            ; Remove the function tied to onbeforeunload and defeat the trap so that 
            ; normal navigation, refresh or close can occur
            IEEval($oIE, "window.onbeforeunload = '';")
            $oIE.quit
    EndSwitch
EndFunc

Func IEEval($o_object, $s_eval)
    Return $o_object.document.parentwindow.eval($s_eval)
EndFunc

Func IEHeadInsertScript($o_object, $s_script)
    Local $o_head, $o_element
    $o_head = _IETagNameGetCollection($o_object, "head", 0)
    $o_element = $o_object.document.createElement('script')
    $o_element.type = 'text/javascript'
    $o_element.text = $s_script
    Return $o_head.appendChild($o_element)
EndFunc

Dale

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

  • 2 weeks later...

Not to be picky but (_IE)Atatch?? :)

Oh, ok, glad you're not being picky.

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...