Back to my page

Developers

You are here: Netvibes Developers Network » Universal Widget API » UWA Frequently Asked Questions
Table of Contents
  • UWA Frequently Asked Questions
    • "How to enable Ajax requests in Standalone mode?"
    • "How to tell if my widget is running in standalone mode?"
    • "My widget appears blank in iGoogle"
    • "How to avoid iGoogle's cache while developing my UWA widget?"
    • "Shouldn't there be something at www.netvibes.com/ns/ ?"
    • "Why can't a retrieve the user cookie in UWA?"
    • "Why doesn't the 'list' type preference display when I test my widget?"
    • "Why can't I use getElementById?"
    • "How can I make inline events work? (onclick, onmouseover, onfocus...)"
    • "What is the type of the value returned for a boolean preference?"
    • "DOM methods are not available for certain HTML elements."
    • "How do I prevent target="_blank" being automatically added to my links?"

UWA Frequently Asked Questions

"How to enable Ajax requests in Standalone mode?"

You need to set up your own local proxy, which will emulate the UWA Environment's one. In PHP, a very minimal proxy would look like this:

<?php
// filename: ajaxProxy.php
header("Content-Type: text/xml");
if ( substr($_GET['url'], 0, 7) == '' ) {
  $handle = fopen($_GET['url'], "rb");
  while ( !feof($handle) ) {
    echo fread($handle, 8192);
  }
  fclose($handle);
}
?>

Once that file is created on the server where your widget lies, you just have to add these lines at the start of your widget's JavaScript code:

if (document.location && document.location.hostname == 'localhost') {
  UWA.proxies.ajax = 'ajaxProxy.php';
}

Use the whole block of code (including the if (… line and the closing }), or else your code will only work in standalone mode.

You should place in such a way that it is called before any Ajax request. Usually, you just need to put it as the first line of your script…

Nota: proxy and widget must be on the same domain.

"How to tell if my widget is running in standalone mode?"

Check for the existence of the standalone variable in widget.environment. Here is a handy test:

if (typeof widget.environment.standalone != "undefined") {
  // we are in standalone mode
}

"My widget appears blank in iGoogle"

The XML parser might be at fault here. If you have a CDATA section, like this:

<script type="text/javascript">
<![CDATA[<END  GeSHi>
var MyWidgetname = { }
 
MyWidgetName.parse = function(data) {
  // your parsing code
  }
<BEGIN GeSHi>]]><END  GeSHi>
</script>

…then try removing them - even if they are commented out. Please allow 1-2 hours for Google's cache to update.

This bug also happens with commented-out CDATA sections:

//<![CDATA[ 
someFunction = function() { ... }
//]]>

"How to avoid iGoogle's cache while developing my UWA widget?"

Google has built a special developer gadget that let's you view the gadgets on your iGoogle page (including your own). You can then uncheck the “Cached” setting of your widget, so that you may directly see the changes you make while working on them.

Click on the following button to add this gadget to your iGoogle page.

"Shouldn't there be something at www.netvibes.com/ns/ ?"

UWA widgets use a new namespace, intended at the widget tag. That namespace uses the URI www.netvibes.com/ns/. At the time of writing, this URI returns a 404 HTTP error: no document resides there.

As said in the XML Namespace specification: “The namespace name, to serve its intended purpose, SHOULD have the characteristics of uniqueness and persistence. It is not a goal that it be directly usable for retrieval of a schema (if any exists).”

While we might put a specific page at the namespace URI sometimes in the future, that URI is already usable as it is: its intent is to give a unique and persistent URI, which it does already.

"Why can't a retrieve the user cookie in UWA?"

While the Mini-Module API allowed for a lot of freedom JavaScript- and DOM-wise, the UWA API serves a different purpose (namely, to run on various platforms). To ensure maximum portability and security, some functionalities have been removed or simply moved into the widget object.

Along with those, the ability to use cookies is gone: the Mini-API way was kind of a hack, and required dynamic files to set/retrieve the cookie (through PHP methods, for instance). It is now recommended to send and retrieve data through the URL of Ajax requests, targeting a dynamic file if you need.

For instance, you could set up a article.php file to handle these data, and access it this way:

var params;
params += "&id=" + encodeURIComponent(widget.getValue('username'));
UWA.Data.getText('example.org/article.php?m=get' + params, myFunction);

"Why doesn't the 'list' type preference display when I test my widget?"

The standalone mode is the only mode where this doesn't work for now, but it is still cumbersome. Try to test your widget in the Netvibes preview mode:

  • click the Add To Netvibes button
  • click the preview button - you don't have to add it afterwards, if you don't wish to

You can also try to test it through the UWA test module in Netvibes. You might also be able to make your list preference work if you send your widget as an XML file: try to use the .xhtml file extension for instance, or to serve the file as application/xhtml+xml

"Why can't I use getElementById?"

Only part of the disabled window and document objets' methods have been transposed in the UWA widget object. getElementById is not among those, but you can find ways to replace it in this specific howto.

"How can I make inline events work? (onclick, onmouseover, onfocus...)"

JavaScript events should not be used directly inline with HTML tags, but should instead be dynamically assigned to those. Therefore,

<a onclick="myFunction();">Click here!</a>

…should be rewritten dynamically using JavaScript:

var a = widget.body.createElement('a');
a.onclick = myFunction;
a.innerHTML = 'Click here !';
 
// optionally, you could set the href
// but remember that UWA adds target="_blank" to links
a.setAttribute('href', 'alternateUrl.html');

…or you should target the HTML tag using getElementByClass or getElementByTagName:

var a = widget.body.getElementsByClassName("functionLink")[0];
a.onclick = myFunction;

…with such a tag:

<a class="functionLink">Click here!</a>

Please note that dynamic function calls must not end with parenthesis - you should use myFunction instead of myFunction(). If you need to pass parameters to your function, you need to use an anonymous function in-between:

a.onclick = function() { myFunction(parameter); };

"What is the type of the value returned for a boolean preference?"

widget.getValue returns a string for a boolean preference in Netvibes, whereas it returns a boolean value in Google IG.

This is a known bug in the getValue behaviour. While we will fix it in a future version of the API, a workaround can be coded this way:

if ((widget.getValue("hideName") == "false") 
     || (widget.getValue("hideName") == false)) {
     // the code to trigger
}

"DOM methods are not available for certain HTML elements."

UWA reprises the main DOM Element methods, like getParent(), setStyle() or appendChild(). Currently, only the basic widget elements (like widget.body) and the elements you create through widget.createElement() are extended automatically.

To manually extend an element, you can use the function UWA.$element(element).

"How do I prevent target="_blank" being automatically added to my links?"

Try adding the following line to your code:

widget.environment.handleLinks = function(){};

If you want to use your own custom behavior for links, try this:

myLinkTag.onclick = function myLinkFunction() {
  // your code
  return false;
  }
  • Print
  • Send to a friend
  • Add to favorites
  • Last modified: 2012/03/15 13:42
spacer
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.