Sidebar Programming: Microsoft took away alert(), I’m taking it Back!

Developers often find the need to popup some kind of informational message to the user. We’ve all been on web sites and received the “Invalid input, please try again” alert, or the “Delete record. Are you sure?” confirmation dialog. But the JavaScript functions to do this is a Sidebar gadget have been disabled by Microsoft. In this article, I’ll show you how to take them back.

Take for example the following html page:

<html>
<head>
    <style type="text/css">
        body{
        height:60px;
        width:130px;
        margin:0;
        padding:0;
        }
    </style>
    <script type="text/javascript">
    function goWeb() {
        var el = document.getElementById("url");
        if (el.value) {
            window.open(el.value);
        } else {
            alert("Please enter a URL");
        }
    }
    </script>
</head>
<body>
    <input type="text" id="url" style="width:100px" />
    <button onclick="goWeb();">Go</button>
</body>
</html>

Something like this is very common when programming a web page. This sample application allows you to enter a web address and launched it in another browser window when you click “Go”. If you don’t enter anything, you receive an error message.

Let’s add a gadget.xml manifest and turn it into a Sidebar gadget.

<?xml version="1.0" encoding="utf-8" ?>
<gadget>
    <name>Alert Test</name>
    <namespace>MyNamespace</namespace>
    <version>1.0.0.0</version>
    <copyright></copyright>
    <description>Alert Test</description>
    <hosts>
        <host name="sidebar">
            <base type="HTML" apiVersion="1.0.0" src="alert.htm" />
            <permissions>full</permissions>
            <platform minPlatformVersion="0.3" />
        </host>
    </hosts>
</gadget>

When you run the gadget and enter a web address, it launches in web browser just as it should. But, leave it blank and click “Go” and nothing happens. What happened to our error message? Unfortunately, Sidebar framework traps the “alert” method and ignores your request. But why would they do this? It’s not perfectly clear to me, nor is it documented anywhere, but one Microsoft official did state “We don’t want gadgets popping up UI that is unprompted by the user“. I agree with this in theory, but I don’t think Sidebar should make these kinds of decisions for me. So, by adding a single line to the HTML, we get back our coveted alert and confirm methods.

<script src="alert.vbs" type="text/vbscript"></script>

Place this line just above above the closing </head> element. What’s in the magical script, you ask? Well as it turns out, the VBScript MsgBox function does not suffer the same Sidebar censorship as it’s JavaScript cousins alert() and confirm(). I used MsgBox to simulate alert and confirm. Here’s the VBScript code:

'simulate JavaScripts alert() function
sub alert(prompt)
    MsgBox prompt, 48 , "Sidebar Gadget"
end sub

'simulate JavaScripts confirm() function
function confirm(prompt)
    dim res
    res = MsgBox (prompt, 33, "Sidebar Gadget")
    if res=1 then
        confirm = true
    else
        confirm = false
    end if
end function

Now that I’ve shown you how to use alert() and confirm() within a Sidebar gadget, all I ask is that you please don’t abuse it. Personally, I’ve only use this from within a Setting dialog, which I believe is the perfect place for an alert.