最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open window in frame - Stack Overflow

programmeradmin6浏览0评论

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("", "topFrame");
<!DOCTYPE html>

<html lang="en" xmlns="">

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

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.", "topFrame");
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3/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>

Share Improve this question edited Mar 11, 2021 at 14:23 David Malášek 1,4321 gold badge11 silver badges24 bronze badges asked Nov 17, 2015 at 14:21 traktor123traktor123 631 gold badge1 silver badge4 bronze badges 7
  • jsfiddle/5hnc2vzv/1 something like this if you want to do it in javascript. But I see no reason not to do this in HTML directly – online Thomas Commented Nov 17, 2015 at 14:28
  • I just learn about window.open mand and there is possibility to load url into frame but it does not work in my example – traktor123 Commented Nov 17, 2015 at 14:31
  • It will try top open a popup: jsfiddle/cmytz1bx If are on chrome like me you wil see a message in the upper right corner saying the popup is blocked – online Thomas Commented Nov 17, 2015 at 14:34
  • I have removed stars on window mand - on my side it just open google. in new window but not in topFrame – traktor123 Commented Nov 17, 2015 at 14:37
  • stackoverflow./questions/4907843/… – online Thomas Commented Nov 17, 2015 at 14:39
 |  Show 2 more ments

3 Answers 3

Reset to default 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 acplish the same task, or jquery's load function iframe: https://www.w3/wiki/HTML/Elements/iframe jquery load: http://api.jquery./load/

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/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", "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", "leftFrame");
</script>

But it does not work

I did additional research and here is correct answer:

MainWindow.html:

<!DOCTYPE html>
<html lang=" EN" xmlns="http://www.w3/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", "leftFrame");

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

发布评论

评论列表(0)

  1. 暂无评论