1

I'm trying to use a JavaScript variable as the src in a <iframe>, the user will then be able to load other content by choosing scr in a menu, which changes the variable. the default src URL is apps/start.php.

Here some code from my index.php:

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

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

    </iframe>

The problem is that i can't get this first small part working... The <script> tag is in the <head>. And when the src is changing will the new document automatically load?

1
  • appurl is a variable containing a string, so I don't see why you should execute it Commented Apr 18, 2013 at 18:06

4 Answers 4

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;
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks now it works! But if the iframe content document is on another domain?
The iFrame is on your own domain, if you want to access the content of the iFrame of another domain, that is not possible. Because thats cross domain javascripting.
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>

Comments

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

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

</iframe>

Comments

0

Try

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.