Getting URL info

February 28th, 2006

= 4)) {
window.external.AddFavorite(url, title);
} else if (navigator.appName == 'Netscape') {
window.sidebar.addPanel(title, url, '');
} else {
alert('Press CTRL-D(Netscape) or CTRL-T(Opera) to Bookmark');
}
}
//-->

I have a set of complex web pages that are built in a frameset within a frameset (not my idea - this is WebHelp I've output from RoboHelp and that's how it comes out). Fortunately, the JavaScript provided by RoboHelp includes a convenient way of calling a particular page within one of the embedded frames, using parameter values in the URL. For example, if the outermost page is called mainpage.html and the inner page you want to show is called mytopic.html then you can show this topic by calling up mainpage.html#mytopic.html

This provides a way of bookmarking the current page within a complex frameset. But to do this we need 3 things:

1) The complete URL of the main page (which is the grandparent frame of the frame that contains the page you want to bookmark).

2) The file name of the page you want to bookmark.

3) The title of the page you want to bookmark.

The following button shows information about the page. On this page the values for the current page
and the grandparent page are the same, because this page isn't in a frameset. You'll just have to take my word for it
that this works when the current page is embedded in a frame of a frameset that is itself within a frameset.

We can use the information shown in the alert box for the above button, to
build the parameter values that we need to pass to the bookmark function. The following button displays the 'Add Bookmark' or 'Add Favorite' dialog box with the details for this page. Try clicking it - but if you use Firefox I'd recommend you don't actually add the bookmark, because the resulting bookmark will display a framed version of this page.

Here's the bookmark function:

<script language="javascript" type="text/javascript">
<!--
function bookmark(url, title){
  if ((navigator.appName == 'Microsoft Internet Explorer') && (parseInt(navigator.appVersion) >= 4))
  {
    window.external.AddFavorite(url, title);
  }
  else if (navigator.appName == 'Netscape')
  {
    window.sidebar.addPanel(title, url, '');
  }
  else
  {
    alert('Press CTRL-D(Netscape) or CTRL-T(Opera) to Bookmark');
  }
}
//-->
</script>

View the source code to see the onclick values that call this function.

--------------------

I've just had another look at this. OK, so it's a pretty useless example because this page isn't framed. You just have to take my word for it that if this page was in a frame of a frameset within a frameset, clicking the second button would create a bookmark with a URL like: http://domain/path/mainpage.html#mytopic.html

A point to note is that this doesn't work if the file is hosted locally, rather than on a Web server. For this reason, you probably want to hide the button by default and then test the protocol value onload (see the first alert box), and display the button if the protocol is "http:".

Comments are closed.