Alex
Alex

Reputation: 329

How do I set the src to an iframe with jQuery?

My iFrame looks like this:

<iframe id="iframe" name="iframe1" frameborder="0" src=""></iframe>

And my script looks like this:

<script type="text/javascript">
    $(document).ready(function() {
    $('#iframe').attr('src',http://google.com);
})
</script>

I've also tried putting quotes around the url:

<script type="text/javascript">
    $(document).ready(function() {
    $('#iframe').attr('src','http://google.com');
})
</script>

But neither is working.

What am I missing?

Upvotes: 17

Views: 69254

Answers (5)

user3332446
user3332446

Reputation: 79

$("#iframe").attr("src","your url");

this will work fine.

Upvotes: 3

ChrisGheen
ChrisGheen

Reputation: 994

You're not allowed to load www.google.com in an iFrame. Try it with another url.

Load denied by X-Frame-Options: https://www.google.com/ does not permit cross-origin framing.

Upvotes: 4

Nick
Nick

Reputation: 4212

Just call the function with Iframe name and desired url

function loadIframe(iframeName, url) {
    var $iframe = $('#' + iframeName);
    if ( $iframe.length ) {
        $iframe.attr('src',url);   
        return false;
    }
    return true;
}

Ex:

loadIframe("iframe1","http://yahoo.com");

Upvotes: 1

SLaks
SLaks

Reputation: 887413

If you look at the browser's error console, you'll see the real problem:

Refused to display 'https://www.google.com/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

Google doesn't let you do that.

Upvotes: 20

rjg132234
rjg132234

Reputation: 620

<script type="text/javascript">
    $(document).ready(function() {
    $('#iframe').attr('src', 'http://google.com');
})
</script>

Quotes missing on the url.

Upvotes: 9

Related Questions