Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use the onBeforeUnload event with Script#?
    primarykey
    data
    text
    <p>I'm using the Script# library with ASP.NET. I want to listen and respond to the onBeforeUnload browser event. </p> <p>I am currently intercepting the event like this:</p> <pre><code>Window.AttachEvent( "onbeforeunload", OnNavigateAway ); ... private void OnNavigateAway() { Script.Alert("You're leaving."); } </code></pre> <p>But the second parameter to Window.AttachEvent is a DOMEventHandler, which has a return type of void. To use the onBeforeUnload event, I need to be able to return a string value, which the browser uses as the confirmation message.</p> <p>Is there any way to do this without emitting a script literal or hand coding javascript? I would really prefer to stay in the compiled C# -> javascript as much as possible.</p> <p>In case it matters, I'm using version 0.5.5 of the Script# library, which isn't the latest version, but I am restricted to this for now.</p> <hr> <p>UPDATE: DuckMaestro answered my question perfectly, but it still didn't work for me. It is the correct answer to my question in that the compiled Javascript is exactly what I was expecting and wanting. But it doesn't have the desired effect of causing the browser to issue a warning prompt. </p> <p>I do have a work-around, though, in case someone else stumbles across this answer and wants to know how I initially hacked it into working. Instead of this:</p> <pre><code>public delegate string BeforeUnloadDelegate(); ... Window.AttachEvent( "onbeforeunload", (DOMEventHandler) (Object) new BeforeUnloadDelegate(OnNavigateAway) ); </code></pre> <p>I did this:</p> <pre><code>Script.Literal( "window.onbeforeunload = this._onNavigateAway" ); </code></pre> <p>This is bad form for a number of reasons. This will only work on the .debug.js class that Script# generates; Script# changes the names in the release version, so the script emitted by the Script.Literal statement won't match up. Also, it negates a lot of the benefits of using Script# in the first place. (For example, using Visual Studio's refactoring tools to rename OnNavigateAway to something else will leave an orphaned reference in the string.) Also, the C# code declares the method as OnNavigateAway, whereas the Script.Literal has to refer to this._onNavigateAway. </p> <p>Still, if, like me, you're on a deadline and looking for a hack, this is a place to start. If I make any more progress on a more correct version, I'll update this question with the details.</p> <p>Thanks again to DuckMaestro for answering the question I asked.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload