Results 1 to 5 of 5

Thread: loading files with frames

  1. #1
    Join Date
    Oct 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default loading files with frames

    Hi.

    On my website I work with frames.

    Let say that I have a frameset.htm with menu-frame and content-frame.
    Let say, that I have many content-files, each is loading in content-frame.

    What I need is: when I open the content-file, the content-file should call its frameset and open within the frameset. So when I click on my hdd (or open via link or from google):

    - content-file1.htm, web browser will open my frameset.htm with menu-frame and content-file1.htm in content-frame,
    - content-file2.htm, web browser will open my frameset.htm with menu-frame and content-file2.htm in content-frame,
    - content-file50.htm, web browser will open my frameset.htm with menu-frame and content-file50.htm in content-frame,
    - content-file200.htm, web browser will open my frameset.htm with menu-frame and content-file200.htm in content-frame,
    ...and so on.

    What I described was an example of what I need, my website is a little more complex and has a lot of content-files.

    Long time ago I found on the internet some script that worked, but I've lost it and now I can't locate it (I don't remember it's name).

    Can someone help?
    Thx.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Put this on your potential 'orphan' page, in the head (configure its two highlighted variables):

    Code:
    <script type="text/javascript">
    if (window == top)
     (function(){
      var framesetURL = 'index.htm';  //set to quoted address of the frameset page
      var targetFrame = 'targetFrame'; //set to quoted name of the target frame on the frameset page
      var w = window.location, url = framesetURL + '?frame=' + targetFrame + '&page=' +
       (encodeURIComponent? encodeURIComponent(w.href) : escape(w.href));
      w.replace? w.replace(url) : w.href = url;
     })();
    </script>
    Put this on your frameset page, also in its head:

    Code:
    <script type="text/javascript">
    ;(function(){
     var m, qv = function (n) {
      m = (new RegExp('[?&;]' + n + '=([^&;#]*)')).exec(location.search);
      return m? unescape(m[1]) : null;
     }, f = qv('frame'), p = qv('page');
     if(f && p){
      window.onload = function(){
       if(qv.fired) return;
       f = window.frames[f].location;
       f.replace? f.replace(p) : f.href = p;
       qv.fired = true;
      };
     };
    })();
    </script>
    Last edited by jscheuer1; 10-18-2008 at 08:31 PM. Reason: minor script efficiencies
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    krjrs (10-18-2008)

  4. #3
    Join Date
    Oct 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thank you, I tested it and it seems to work perfectly.

    Are there any known issues/problems that can be expected (that I should pay my attentnion to) related to this solution?

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Good question.

    • Obviously, with javascript unavailable or disabled, this will not work. But you will be no worse off than you were without it, and most folks do have javascript enabled. To help with those few cases where javascript isn't active, you could have a noscript tag on the 'orphan' page letting non-javascript users know that they are viewing the page outside of its intended frameset, with a link to that frameset. It wouldn't load the page into the frameset when clicked, but it would at least allow them to get to the frameset if they so choose.

    • The query string variable names I used are frame= and page=, so if your 'orphan' already has a query string and it contains these names as variables, there could be problems. It is unlikely to happen in the scenario you described though. Generally a page only gets a query string if it is the action of a form using the GET method, or if you give it one as part of a link or via javascript. Are you doing that with any of your 'content' (potential 'orphan') pages? Even if you are, it would only matter if you used those variable names in the query string, as they should now be reserved for use with this code. If you need them elsewhere, they could be changed in this code.

    • Your question also made me realize that:

      Where I had (on the 'orphan'):

      Code:
      (encodeURIComponent? encodeURIComponent(w.href) : escape(w.href));
      that if encodeURIComponent was not supported (rare) there would be an error. And that where I did:

      Code:
      return m? unescape(m[1]) : null;
      on the frameset page, though 99% of the time it would be fine regardless of whether the original URI was encoded or escaped, in some odd cases (perhaps where escape sequences were already a part of the URI, or some other unforeseen situation) it could cause a problem, as to be 100% sure of equivalence, the same encode/decode or escape/unescape method must be paired like unto like.

      To overcome the first issue here we must test encodeURIComponent as a property of a universal object to which it would belong if supported (window will do for this), so change the code on the 'orphan' page to:

      Code:
      <script type="text/javascript">
      if (window == top)
       (function(){
        var framesetURL = 'index.htm';  //set to quoted address of the frameset page
        var targetFrame = 'targetFrame'; //set to quoted name of the target frame on the frameset page
        var w = window.location, e = window.encodeURIComponent || escape,
        url = framesetURL + '?frame=' + targetFrame + '&page=' + e(w.href);
        w.replace? w.replace(url) : w.href = url;
       })();
      </script>
      To remedy the other issue here on the frameset page, use (lets the same browser that got you there decide which decoding to use, so it will match the encoding used):

      Code:
      <script type="text/javascript">
      ;(function(){
       var m, d = window.decodeURIComponent || unescape, qv = function (n) {
        m = (new RegExp('[?&;]' + n + '=([^&;#]+)')).exec(location.search);
        return m? d(m[1]) : null;
       }, f = qv('frame'), p = qv('page');
       if(f && p)
        window.onload = function(){
         if(qv.fired) return;
         f = window.frames[f].location;
         f.replace? f.replace(p) : f.href = p;
         qv.fired = true;
        };
      })();
      </script>
    Last edited by jscheuer1; 10-19-2008 at 01:24 PM. Reason: minor code efficiencies
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    krjrs (10-24-2008)

  7. #5
    Join Date
    Oct 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    thanks for detailed explanation.

    I generally feel outdated in internet technologies. the only thing that I am able to implement (except css) to my websites today are javascripts. ;-)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •