PDA

View Full Version : loading files with frames



krjrs
10-18-2008, 12:44 PM
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.

jscheuer1
10-18-2008, 06:23 PM
Put this on your potential 'orphan' page, in the head (configure its two highlighted variables):


<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:


<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>

krjrs
10-18-2008, 11:22 PM
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?

jscheuer1
10-19-2008, 05:25 AM
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'):


(encodeURIComponent? encodeURIComponent(w.href) : escape(w.href));

that if encodeURIComponent was not supported (rare) there would be an error. And that where I did:


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:


<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):


<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>

krjrs
10-24-2008, 10:28 PM
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. ;-)