Join LiveSide!
Sign In using: Name/Password OpenID
or Live ID: Sign In Live ID
0 out of 8,050 members
are online, & 48 guest(s).

Recent Comments

–idom
re: Sidebar Programming: Microsoft took away alert(), I'm taking it Back!

–JAtkins
re: Sidebar Programming: Microsoft took away alert(), I'm taking it Back!

–bf1977
re: Sidebar Programming: Microsoft took away alert(), I'm taking it Back!

–mark_memory
re: Sidebar Programming: Microsoft took away alert(), I'm taking it Back!

Log in or Join to leave a comment!

LiveSide on Mobile

Our latest posts and our favorite links,
all on your phone or mobile device
Visit m.LiveSide.net
Or go to www.LiveSide.net
on your mobile device and we'll redirect you!

Tweets We Like

Loading...

LiveSide Time

Redmond

Dallas (server)

London

Shanghai

Windows Live Calendar

PDC LA Nov 17-19

   www.microsoftpdc.com

Follow us on Facebook

    LiveSide.net
   
    Promote Your Page Too

  • LiveSide on the Windows Live Network
  • LiveSide on Facebook
  • LiveSide on Twitter
  • LiveSide RSS  
  • Windows Live Alerts
  •  
  • feedburner
  •  
LiveSide - Developer Blog

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.

Comments

mark_memory wrote re: Sidebar Programming: Microsoft took away alert(), I'm taking it Back!
on Tue, Dec 26 2006 5:23 AM

have not received your newsletter for days,i thought you were on your christmas holiday,thanks for always being there.

and happy new year.

bf1977 wrote re: Sidebar Programming: Microsoft took away alert(), I'm taking it Back!
on Wed, Dec 27 2006 11:30 PM

this could not be decided with use of the Ajax with C# or VB.net?

JAtkins wrote re: Sidebar Programming: Microsoft took away alert(), I'm taking it Back!
on Wed, Apr 11 2007 11:45 PM

Just wanted to say thanks for the workaround.  The missing alert functionality was bugging me.  I use it for debugging all of the time.

idom wrote re: Sidebar Programming: Microsoft took away alert(), I'm taking it Back!
on Tue, May 1 2007 9:49 AM

Thanks Donavon

This could be very help full, but I don’t understand how I activate this VB code thru my JavaScript code?

Ido

myspace music videos codes for fall out boy wrote myspace music videos codes for fall out boy
on Tue, Sep 18 2007 3:48 PM

myspace music videos codes for fall out boy

Sign In Live ID using Live ID, Name/Password, or OpenID
or Join LiveSide to leave a comment!