Team LiB
Previous Section Next Section

Browser Control

Once we have mastered detecting visitors’ browsers and their various features, we might be interested in trying to control these browsers. Using the Window object as discussed in Chapter 12, it is possible of course to change window appearance. For example, we might scroll or resize the window using window.scrollTo() or window.resizeTo() or set the browser status message using window.status or window.defaultStatus. For more control, we might consider opening a new window (window.open) and removing the browser’s chrome or even going full screen. We could even send the user to another page using window.location or use timeouts and intervals (window.setTimeout and window.setInterval) to perform activity at set moments. Yet we can even go beyond these possibilities in some instances using proprietary features of JavaScript in Netscape and Internet Explorer.

Simulating Browser Button Clicks

Netscape and Opera support numerous methods that allow the developer to fake various browser activities, such as clicking a particular button. Internet Explorer doesn’t support very many of these browser control methods, but it does not support probably the most useful one, window.print( ), which triggers the printing of the page. For Internet Explorer users we can, however, use object detection to make an example that will at least not throw an error:

<<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">>
<<html xmlns="http://www.w3.org/1999/xhtml">>
<<head>>
<<title>>Browser Button Simulator<</title>>
<<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />>
<</head>>
<<body>>
<<h1 align="center">>Button Simulator<</h1>>
<<hr />>
<<form action="#" method="get">>
<<input type="button" value="PRINT" onclick="if (window.print) window.print();" />>
<<br />><<br />>
<<input type="button" value="FORWARD" onclick="if (window.forward)
 window.forward();" />>
<<br />><<br />>
<<input type="button" value="BACK" onclick="if (window.back) window.back();" />>
<<br />><<br />>
<<input type="button" value="HOME" onclick="if (window.home) window.home();" />>
<<br />><<br />>
<<input type="button" value="STOP" onclick="if (window.stop) window.stop();" />>
<</form>>    
<</body>>
<</html>>

Given that some buttons can be simulated, you might wonder if it is possible to control other aspects of the user’s browser such as their preferences. The next section introduces this idea by trying to set the user’s default home page using JavaScript.

Preference Setting: Specifying the Home Page

Doing something that may affect the user’s browser setup is potentially hazardous, and each browser takes a different approach to this issue. Some just downright disallow it, others require permissions, and yet others prompt the user. For example, under Netscape, you are required to ask for permission to read and write the values of a user’s browser preferences. Take a look at this simple example to see how to set the home page of a user:

<<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">>
<<html xmlns="http://www.w3.org/1999/xhtml">>
<<head>>
<<title>>Navigator Preference Tester<</title>>
<<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />>
<<script type="text/javascript">>
<<!--
function setHomePage()
{
 if ((window.netscape) && (window.netscape.security))
  {
   netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesRead');
   var home = navigator.preference('browser.startup.homepage');
   if (home != 'http://www.pint.com/')
    {
    netscape.security.PrivilegeManager.enablePrivilege('UniversalPreferencesWrite');
    navigator.preference('browser.startup.homepage','http://www.pint.com/');
    }
  }
}
// -->>
<</script>>
<</head>>
<<body>>
<<form action="#" method="get">>
<<input type="button" value="Set Home Page Preference" onclick="setHomePage();" />>
<</form>>
<</body>>
<</html>>

Given the danger involved in setting preferences, when you access the privilege manager from Netscape, you should see a dialog like the one shown here:

Click To expand

Internet Explorer uses a very different method to access browser preferences like the home page setting. Under the 5.x release and beyond, you can use a default JavaScript behavior to set the home page:

<<a href="#"
  onclick="HomePage = 'http://www.pint.com'; 
this.style.behavior='url(#default#homepage)';this.setHomePage(HomePage);
return false">>Set PINT to your home page<</a>>

Fortunately, as with Netscape, you will be prompted if you want to do this, so a rogue site just can’t slam your current settings without your permission.

Click To expand

There are similar techniques for setting the user’s bookmarks and other preferences, but designers should think twice before taking such drastic control of a user’s browsing experience.


Team LiB
Previous Section Next Section