Đinh Thế Lâm
Đinh Thế Lâm

Reputation: 57

What is the best way to set URL for <frame> in <frameset>

I'm working with frameset and frame right now and I'm so confused about how to change frame URL. My customer they using either location or src wherever they like to set/change frame URL. Sometime location work, sometime src work so I'm going to find out what's going on.
I create 3 html files: parent.html, left.html, right.html. Here is the parent.html content

<frameset cols="*,*">
   <frame id="left" src="left.html">
   <frame id="right" src="right.html">
</frameset>

Here is the left.html content

<head>
    <script>
        function LocationThis() {
            window.frames['innerleft'].location = "http://prntscr.com";
        }
        function SrcThis() {
            window.frames['innerleft'].src = "http://prntscr.com";
        }
        function LocationOther() {
            window.parent.frames['right'].location = "http://prntscr.com";
        }
        function SrcOther() {
            window.parent.frames['right'].src = "http://prntscr.com";
        }
    </script>
</head>
<body>
    <h1>Left</h1>
    <button type="button" onclick="LocationThis();">Location this</button>
    <button type="button" onclick="SrcThis();">Src this</button>
    <button type="button" onclick="LocationOther();">Location other</button>
    <button type="button" onclick="SrcOther();">Src other</button>
    <hr/>
    <iframe id="innerleft" src="" />
</body>

Only src work in this case. Then why location work sometime but src won't? Ok, now I change script like this

//window.parent.frames['right'].location = "http://prntscr.com";
window.parent.frames[1].location = "http://prntscr.com";

Then location work fine. So frames['right'].src and frames[1].location then. But not all the case, sometime frames['right'].location work too. When I remove id and only using name for frame I must use location

<frame name="right" src="right.html">

That's it. Oh and thanks to IE refuse to take src so I must checking like this

try {
  window.parent.frames["id"].src = {url};
}
catch(er) {
  window.parent.frames["id"].location = {url};
}

Is there a neat solution for this whole things?

Upvotes: 1

Views: 3337

Answers (1)

Hugo Silva
Hugo Silva

Reputation: 6938

I can't actually confirm this, because on Chrome it doesn't even work (check out de docs on the window.frames object), but I am assuming that getting the frame by ID will return a reference to the HTML element, which has a src property - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/frame

And when you do it properly (getting by index), you get a reference to the window object of that frame, which has a location property - https://developer.mozilla.org/en-US/docs/Web/API/Window

So, my guess is that window.frames['id'].src works in some cases because of some quirk browser implementation, but in reality your options are:

  1. window.frames[index].location
  2. document.getElementById('FrameID').src

Upvotes: 1

Related Questions