Question

I'm working on my website and the main page (index.html) is composed of two frames: page1.html and page2.html. This is the code:

<!DOCTYPE html>
<head>  
<frameset rows="16%,84%" framespacing="0" border="0" frameborder="0">
  <frame name="page1" target="page2" src="page1.html" scrolling="auto">
  <frame name="page2" src="page2.html" target="_self" scrolling="auto">
<noframes></noframes>
</head>
</html>
  • The first one works as a fixed content of the page that contains multiple links to the rest of the site. It is absolutely important that this page remains open every moment.

  • The second is the "body", the content area, wich is replaced when any of the links are clicked.

So here's my problem: if any page is accessed not from index.html, for example, google, page1.html will not exist, since it only opens through index.html and the site will be missing it's main links.

I'm looking for a code that will check if the page "page1.html" is present and if it's not, open it as a frame.

The closest I've got was this javascript code:

<script language="javascript">
window.onload=function(){
    if(self==parent)
        window.location.href = "index.htm"
}
</script>

It'll redirect any user who enters a page if not from index.html, to it. But that's still no good. Any clues?

Was it helpful?

Solution

When I read your question, first thing that crossed my mind was to read and act upon window.frames.length. Exactly what you have done yourself.

However, you could pretty much see a frameset and frame as deprecated technologies nowadays. Instead, put the links in a HTML5 <nav> or regular <div>, then put the content in a HTML5 <article> or another regular <div>. Next load the content dynamically using vanilla/plain JavaScript or a framework such as jQuery.

If you insist on putting the navigation links in a "frame" that never goes away, and you want to load HTML files into a sub part of the screen, then I'd still put the navigation links in a <nav> or <div> and use an <iframe> for the content.

OTHER TIPS

Frames are deprecated but anyways:
Check if the page is a parentframe like this:

if(window.top == window.self)

window.top is the parent frame and window.self is the current frame. It they are equal, there is no parentframe(set).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top