SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Frame Master : Dynamic Solution

To make this solution really work, both the code on the page and the frameset needs to be modified. Instead of pointing to a static frameset document, we modify the frameset document to generate itself dynamically based on any supplied search arguments. In the page itself, we set the search argument to point at the URL of the currnet page:

  var container = "demo.htm"
  var wname = top.location.pathname.substring((top.location.pathname.lastIndexOf("/"))+1).toLowerCase()
  if (wname!=container)
    parent.location.replace(container + "?contents="+escape(this.location))

Notice that the location is escaped. This is required to ensure you are sending a correct URL. In the frameset where we check for the contents argument, we unescape the string. To get the best results, we have found that the entire frameset needs to be generated through script:

  function getPage() {
    return unescape( window.location.search.substring(window.location.search.indexOf("=")+1))
  }

  document.write("<FRAMESET ROWS=\"100,*\">")
  if (window.location.search=="")  
    document.write("<FRAME NAME=\"contents\" SRC=\"page1.htm\">")
  else 
    document.write("<FRAME NAME=\"contents\" SRC=\""+getPage()+"\">")
  document.write("</FRAMESET>")

Now, when the frameset is loaded it checks whether the a spcecific content file is specified or it loads the default page. Now you have seen how to automatically reload pages into their appropriate context. However, this design requires an extra round-trip. Depending upon your site, you may not mind if a document is viewed outside the frameset and instead allow the user to manually choose if they want the frameset retrieved. When using the dynamically generated frameset, you just need to add the appropriate link to the document:


  <A HREF="demo.htm?contents=page2.htm>View in Frame</A>

To bring this all together, below we dynamically generate the link only when required.


  var container = "demo.htm"
  var wname = top.location.pathname.substring((top.location.pathname.lastIndexOf("/"))+1).toLowerCase()
  if (wname!=container)
    document.write("<A HREF=\""+container+"?contents=" + escape(window.location) + "\">View in Frame</A>")

In our final demonstration, you will be presented a page outside of a frameset. You can then click on view frames to redisplay the frameset with the appropriate context.

Discuss and Rate this Article