Alert JavaScript

Hello, i have a problem making an alert with javascript, when the user left the portlet and goes to another from the menu application the alert never pop up, is anyway to do this so i can alert to an user that it is leaving the portlet.? Thank you.

Please provide as much information as possible regarding what you have already tried.

I have this different script

 var changes = false;        
        window.onbeforeunload = function() {
            if (changes)
                var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
                if (confirm(message)) return true;
                else return false;

and this one


but one i leave the portlet nothing happen.

Which version of MWS are you using?

The version is 8

Ok. Try something like this:

//register your custom handler if it is not already there.
if (!window.YourUnloadHandler) {
     * YourUnloadHandler functions.
    window.YourUnloadHandler = {
        // flag to workaround IE bugs
        skipNextUnload: false,
         * Click handler to detect phatom beforeunload events in ie.
         * @param {event} event Event.
        skipUnloadForScriptUrls: function(event) {
            var H = YourUnloadHandler;
            var target = Event.findElement(event);
            // check if self, parent, or grandparent is a link
            for (var i = 0; i < 3 && target.tagName != "A" && target.parentNode; i++)
                target = target.parentNode;
            H.skipNextUnload = (/^(javascript|mailto|tel|fax|aim|geo)/.test(target.href));
         * Onbeforeunload handler
         * @param {event} event Event.
        unloadFn: function(event) {
            if (console)
                console.log("unloadFn called");

            var H = YourUnloadHandler;
            if (H.skipNextUnload) {
                if (console)
                   console.log("skipped unload event");

                H.skipNextUnload = false;
    // register your custom event handler
    Event.waitUntilLoaded(function() {
        var H = YourUnloadHandler;

        if (console)
            console.log("registering event handlers");

        //observer for full page reload
        Event.observe(window, "beforeunload", H.unloadFn);

        //observer for partial page reload
        OpenAjax.hub.subscribe("", H.unloadFn);

        //special care to workaround IE wierdness.
        if (
            Event.observe(document, "click", H.skipUnloadForScriptUrls);

It almost work, the one problem is that now when i change the portlet it pop up the alert in every portlet lol, i have to control that this behavior is only for one portlet.

I just need to modify that javascript, so it only works in a specify portlet.