4

I learn JavaScript now, and encounter with next problem: I have page with some frames, and I want to load some page into one of specified frames; But code below does not do what I want.

Could you suggest please how could I load any url into specified frame?

//"use strict";
function print(str) {
  document.write("<p><pre>" + str + "</pre></p>");
}

window.open("http://www.google.com", "topFrame");
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta charset="utf-8" />
  <title>Frameset Example</title>
</head>
<frameset rows="160,*">
  <frame name="topFrame">
    <frameset cols="50%,50%">
      <frame name="leftFrame">
        <frame name="rightFrame">
    </frameset>
</frameset>

<body>
</body>

</html>
Daweed
  • 1,419
  • 1
  • 9
  • 24
traktor123
  • 63
  • 1
  • 1
  • 4

3 Answers3

1

In order to open a page within a frame, set the src attribute of the frame to the desired url.

<frame src="whateversite.html">

Note that frames are obsolete in HTML5. Consider using an iframe to accomplish the same task, or jquery's load function iframe: https://www.w3.org/wiki/HTML/Elements/iframe jquery load: http://api.jquery.com/load/

Eric Phillips
  • 866
  • 7
  • 23
  • That is good, but how could I dynamically load google.com in some frame specified by name? – traktor123 Nov 17 '15 at 14:45
  • not supported frames is not the case because we could switch to html 4 and result the same – traktor123 Nov 17 '15 at 14:51
  • document.getElementById('myframe').src="www.mywebsite.html" I only mentioned that they are obsolete so you can consider alternate options moving to future proof your page. Also I believe google prevents you from loading their page into a frame, http://stackoverflow.com/questions/8700636/how-to-show-google-com-in-an-iframe If google was only for the sake of example you should be fine with your real site. – Eric Phillips Nov 17 '15 at 14:55
0

Seems that I found why url does not loaded into frame 1) script is loaded before frames and it does not know about topFrame 2) I could do this loading from child frames for example like this

MainWindow.html:

<!--<!DOCTYPE html>-->  
<html lang=" EN" XMLNS="http://www.w3.org/1999/xhtml">
<head>
    <title>Frameset Example</title>
</head>
<frameset rows="160,*">
    <frame src="frame.html" name="topFrame"/>
    <frameset cols="50%,50%">
        <frame src="frame.html" name="leftFrame"/>
        <frame src="frame.html" name="rightFrame"/>
    </frameset>
</frameset>
<body>
</body>

frame.html:

<!DOCTYPE html>
<html>
<head>
    <title>Frameset Example</title>
    <script>   
        window.open("http://korrespondent.net", "leftFrame");
    </script>
</head>
<body>
</body>
</html>

But I still have question: how to run the script from MainWindow.html? I want put next script into somewhere in MainWindow.html:

<script>   
    window.open("http://korrespondent.net", "leftFrame");
</script>

But it does not work

traktor123
  • 63
  • 1
  • 1
  • 4
0

I did additional research and here is correct answer:

MainWindow.html:

<!DOCTYPE html>
<html lang=" EN" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Frameset Example</title>
    <script type="text/javascript" defer src="LoadFrame.js"></script>
</head>
<frameset rows="160,*">
    <frame src="frame.html" name="topFrame" />
    <frameset cols="50%,50%">
        <frame src="frame.html" name="leftFrame" />
        <frame src="frame.html" name="rightFrame" />
    </frameset>
</frameset>
<body>
</body>
</html>

frame.html:

<!DOCTYPE html>
<html>
<head>
    <title>Frameset Example</title>
</head>
<body>
</body>
</html>

LoadFrame.js:

window.open("http://www.korrespondent.net", "leftFrame");

So to have possibility access to some inner frame we should use deferred loading of JavaScript

traktor123
  • 63
  • 1
  • 1
  • 4