views:

138

answers:

1

I've been working on creating a Windows Desktop Gadget. I can create HTML elements I want with document.createElement. I can set their attributes and I can get those attributes after setting them, but anything I do to modify the document just fails completely silently. I'm not seeing any errors at all, but the gadget just doesn't change. Even something as simple as document.body.innerHTML = "asdf"; does absolutely nothing. The same code in IE8 works perfectly fine. What am I missing here?

gadget.xml:

<?xml version="2.0" encoding="utf-8" ?>
<gadget>
<name>Citrix Logon Controller</name>
<version>0.1</version>
<author name="Brian G. Shacklett">
    <info url="http://digital-traffic.net" />
</author>
<copyright>&#169; Brian G. Shacklett</copyright>
<description>Controls Citrix Logons.</description>
<hosts>
    <host name="sidebar">
        <base type="HTML" apiVersion="1.0.0" src="gadget.html" />
        <permissions>Full</permissions>
        <platform minPlatformVersion="1.0" />
    </host>
</hosts>
</gadget>

HTML:

<html>
    <head>
    <title>Citrix Manager</title>
    <script type="text/javascript">
        debugger;
    </script>
    <script src="scripts/debug.js" type="text/javascript"></script>
    <script src="scripts/gadget.js" type="text/javascript"></script>
    <script src="scripts/util.js" type="text/javascript"></script>
    <link type="text/css" rel="stylesheet" href="styles/gadget.css" />
</head>

<body >
    <div id="header"><h1>Citrix Manager</h1></div>
    <div id="mainContent">
        <a href="#" onClick="window.location.reload()">reload</a><br />
        <a href="#" onClick="testFunction();">New Server</a>
    </div>
</body>
</html>

gadget.js:

function testFunction()
{
document.body.innerHTML = "asdf";
}
+1  A: 

Your code looks fine, and I have used innerHTML and link onclicks just fine many times. I would check two things.

Directly above your document.body.innerHTML = "asdf"; line, put window.prompt("","");. This will allow you to verify the function is even being called:

function testFunction() 
{
    window.prompt("", "");
    // window.prompt("", document.body.innerHTML);    // before
    document.body.innerHTML = "asdf";
    // window.prompt("", document.body.innerHTML);    // after
} 

You can also add some before/after prompts to give you the value of innerHTML (see commented code).

Secondly, I would check for any transitions you might be using. In Vista System.Gadget.beginTransition() will lock the appearance of the gadget until System.Gadget.endTransition() is called. If an error is thrown in between these two functions, the endTransition() is never called and the gadget will locked up.

System.Gadget.beginTransition();
blah = blah.blah.blah.blah;         // "blah" is null or not an object
System.Gadget.endTransition(System.Gadget.TransitionType.morph, 1.0);

It's always safest to use a try/catch around what's in between. Other than that I can't say what might be causing your DOM issues, but my first suggestion (the prompt) might give you an idea of what the problem is.


UPDATE

Should have spotted this before. You need to cancel the default action, using return false;, of the link click:

   <a href="#" onClick="testFunction(); return false;">New Server</a> 

After your innerHTML function is run, the page is navigated to "#". I didn't think this would be a problem, since # usually takes you to a page anchor without reloading but it looks like this is the cause of the problem here. Fully tested.

Andy E
I tried your suggestion and output the value of document.body.innerHTML after changing it. The value shows as "asdf" as it should, but the widget's interface isn't updating. Is there a redraw function that needs to be run for widgets after a DOM update?
bshacklett
@bshacklett: nope it means most likely there's an unended transition somewhere (see second part of my post). That's the only thing that can stop the window from redrawing whilst allowing JavaScript code to execute. I would check all your files and comment out any transition code for now.
Andy E
I'm sorry, I forgot to mention that the only code that is running is "testFunction()", which is listed above. I got rid of everything else.
bshacklett
@bshacklett: found the problem, see my update.
Andy E
That did it! Thanks. that was driving me crazy!
bshacklett