Javascript 如何打开框架中的窗口

Javascript 如何打开框架中的窗口,javascript,html,Javascript,Html,我现在学习JavaScript,遇到下一个问题: 我有一个带有一些框架的页面,我想将一些页面加载到一个指定的框架中; 但下面的代码并不是我想要的 您能建议我如何将任何url加载到指定的框架中吗 //“严格使用”; 功能打印(str){ 文件。写(“”+str+“”); } 窗口打开(“http://www.google.com“,”顶框“) 框架集示例 要打开框架内的页面,请将框架的src属性设置为所需的url <!--<!DOCTYPE html>--> <

我现在学习JavaScript,遇到下一个问题: 我有一个带有一些框架的页面,我想将一些页面加载到一个指定的框架中; 但下面的代码并不是我想要的

您能建议我如何将任何url加载到指定的框架中吗

//“严格使用”;
功能打印(str){
文件。写(“”+str+“

”); } 窗口打开(“http://www.google.com“,”顶框“)

框架集示例

要打开框架内的页面,请将框架的src属性设置为所需的url

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

请注意,框架在HTML5中已过时。考虑使用IFRAME来完成相同的任务,或者jQuery的加载函数 iframe:
jquery加载:

似乎我找到了url不加载到框架中的原因 1) 脚本在帧之前加载,它不知道topFrame 2) 我可以从子帧加载,比如这样

MainWindow.html:

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

框架集示例
frame.html:

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

框架集示例
窗口打开(“http://korrespondent.net“,”左框“);
但我仍然有一个问题:如何从MainWindow.html运行脚本? 我想将下一个脚本放到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>

窗口打开(“http://korrespondent.net“,”左框“);

但是它不起作用

我做了额外的研究,下面是正确的答案:

MainWindow.html:

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

因此,为了能够访问一些内部框架,我们应该使用JavaScript的延迟加载,如果您想在JavaScript中这样做的话。但我觉得没有理由不直接在HTML中这样做。我只是了解window.open命令,有可能将url加载到框架中,但在我的示例中它不起作用。它将尝试顶部打开一个弹出窗口:如果你像我一样在chrome上,你会在右上角看到一条消息,说弹出窗口被阻止。我已经删除了window命令上的星星-在我的浏览器上只需在新窗口中打开google.com,但不在TopFrame中打开就可以了,这很好,但是我怎么能在由名称指定的某个框架中动态加载google.com呢?不支持的框架不是这样的,因为我们可以切换到html 4并生成相同的文档。getElementById('myframe')。src=“www.mywebsite.html”我只是提到他们已经过时了,所以你可以考虑替代选项移动到未来证明你的网页。而且我相信谷歌阻止你将他们的页面加载到一个框架中,如果谷歌只是为了举例,你应该对你的真实网站很满意。