Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 566
Alex Avatar asked Jun 06 '13 20:06

Alex


People also ask

Can you use jQuery in an iframe?

However, it's also possible to use a really simple jQuery to access the elements within the iFrame. Check it out below: $(document). ready(function(){ var iFrameDOM = $("iframe#frameID").

How add src attribute in jQuery?

Answer: Use the jQuery attr() Method You can use the attr() method to change the image source (i.e. the src attribute of the <img> tag) in jQuery. The following example will change the image src when you clicks on the image.

What is the SRC attribute in iframe?

The HTML <iframe> src attribute is used to specify the URL of the document that are embedded to the <iframe> element. Attribute Values: It contains single value URL which specifies the URL of the document that is embedded to the iframe.


2 Answers

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.

like image 59
SLaks Avatar answered Oct 01 '22 01:10

SLaks


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

Quotes missing on the url.

like image 24
rjg132234 Avatar answered Oct 01 '22 00:10

rjg132234