2

I have this in a javascript file to diguise the source as much as possible

$(document).ready(function () {
    document.getElementById('myframe1').src = "http://www.mysite.com/index.html";

});

and in the html

<iframe id="myframe1" width="900" height="700" src=""></iframe>

Sorry I am a bit of a notice with javascript. Any help with be great! thanks.

2
  • 1
    Are you using jQuery or plain javascript? Your code says one thing and your tagging say another. Commented Oct 1, 2012 at 10:43
  • 1
    tbh I didn't know. Glad there are both options here though. Commented Oct 1, 2012 at 11:23

1 Answer 1

7

With jQuery:

<script type="text/javascript">
    $(document).ready(function () {
        $("#myFrame1").attr("src", "http://www.mysite.com/index.html");
    });
</script>

With plain javascript:

<script type="text/javascript">
    window.onload = init;

    function init() {
        document.getElementById('myframe1').src = "http://www.mysite.com/index.html";
    }
</script>

You can add the function to the onload event like above.

Sign up to request clarification or add additional context in comments.

3 Comments

With plain JavaScript I'd wrap the code in a function that's run when window.onload occurs, unless the script is at the bottom of the page.
Did it help you or are you stuck with something?
it works when I put the (non-jquery) javascript directly in the code, but when I put it in a separate js file (eg. <script type="javascript/text" src="blah.js"></script>) with "blah.js" containing all of the non-jquery javascript above, it doesn't.

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.