1

我正在尝试使用 JavaScript 变量作为srca <iframe>,然后用户将能够通过scr在更改变量的菜单中进行选择来加载其他内容。默认的 src URL 是apps/start.php.

这是我的一些代码index.php

    <script>
        var appurl = "start.php";
        document.getElementById("app").src = "apps/" + appurl();
    </script>

    <iframe id="app" src="">

    </iframe>

问题是我无法让这第一小部分工作......<script>标签在<head>. 当src它发生变化时,新文档会自动加载吗?

4

4 回答 4

1

The problem is that the Javascript is run before the iframe is there. Put your javascript below your iFrame or inside the document ready and it will work.

Example document onload:

<script>
    window.onload = function ()
    {
        var appurl = "start.php";
        document.getElementById("app").src = "apps/" + appurl();
    }
</script>

<iframe id="app" src="">

</iframe>
于 2013-04-18T18:06:37.840 回答
1

Wait for the page to load (note that this is only going to work when the iframe is on your domain)

window.onload = function(){
 var appurl = "start.php";
 document.getElementById("app").src = "apps/" + appurl;
};
于 2013-04-18T18:07:17.100 回答
0
<script>
    window.onload = function ()
    {
        var appurl = "start.php";
        document.getElementById("app").src = "apps/" + appurl;
    }
</script>

<iframe id="app" src="">

</iframe>
于 2013-04-18T18:08:19.370 回答
0

尝试

<script>
window.onload = function(){
 var appurl = 'start.php';
 document.getElementById("app").src = "apps/" + appurl;
};
</script>
于 2013-04-18T18:10:39.980 回答