Tip: Open default browser from the HTML details view

Wednesday, April 23, 2008 at 4:34 PM



When you use the HTML details view to display information, on Windows every link inside is opened using Internet Explorer, even if the user has another browser like Firefox or Opera set as their default browser. However, you can fix this with a few lines of code in your gadget.

The following link opens the Google web page in Internet Explorer in Windows — even if another browser is set as the default.
<a target="_blank" href="http://www.google.com">Go to Google</a>
To remedy this, open the URL using JavaScript and the onclick event.
<a target="_self" href="#"
onclick="window.external.openUrl('http://www.google.com');">
Go to Google</a>
This executes the openUrl method on the external object with the parameter of the URL you want to visit. What does this external object look like? Here is the JavaScript code that defines the external object and assigns it to the details view. This should be run before the details view is opened.
var externalObject = new Object();
externalObject.openUrl = function(url) {
framework.openUrl( url );
};
mydetailsview.external = externalObject;
Now all your links will be opened in the default browser. Just don't forget to escape the URL!

Have a tip you'd like to share? Send it to gd-developer AT google DOT com. To see all tips, choose the Tips label.

4 comments:

EGSnyder said...

How do I find the coding for the desktop "gadget"? I'd like to make the change so it will open Firefox instead of IE.

/egs

Unknown said...

I need to open the url in a default browser. But the code above is not executing properly. I am using directly the code in java script and it's not executing. Please let me know what I am doing wrong:
tbxss is the one which is passed in javascript function:

var externalObject = new Object();
externalObject.openUrl = function(tbxss) {
framework.openUrl( tbxss );
};
window.external = externalObject
}
]]>


Or in directly calling in my function:

window.external.openUrl('http://www.google.com');

But it doesn't work at all.

window.open works fine but only in Firefox.. So please help me...

deveras said...

Insufficient information. No example. Tried the code, didn't work. Please update this!

Ben said...

This code doesn't seem to work...