Is an empty iframe src valid?

I want the iframe to initially have src as empty, and then after loading the page; call the JS function and then set the src value to the actual value.

So, <iframe src="#" /> valid OR I need to use something like javascript:; etc.

+48
javascript html css iframe
May 10 '11 at 7:27
source share
7 answers

just <iframe src='about:blank'></iframe>

+69
May 10 '11 at 7:29 a.m.
source share

HTML 5 working draft, section 4.8.2 , says (my attention):

The src attribute gives the address of the page in which the subview should contain the context. The attribute, if present, must be a valid non-empty URL , potentially surrounded by spaces.

According to RFC 3986 , valid URLs must begin with the name of the scheme, and relative links may not only consist of a fragment .

Therefore, # not a valid URL and should not be used as the value of the src attribute.

Use about: blank instead.

+41
May 10 '11 at 7:35
source share

No, it is incorrect to specify an empty iframe src.

You should use <iframe src="about:blank" /> .

# is a link to the binding to the current page (or is often used as a routing scheme when working with AJAX requests). Using it as an iframe source would be pointless since the iframe does not link to the content on the current page and is not used with AJAX requests.

about:blank is a cross-browser standard for displaying a blank document.

June 8, 2012 update:

It seems that the 'living' spec no longer renders the iframe invalid if the src attribute is missing:

If, when the element is created, the srcdoc attribute is not set and the src attribute is also not set or not set, but its value cannot be resolved, the browsing context will remain on the initial about: blank page.

If both of these attributes are not set, the default view context will be about:blank . To ensure proper backward compatibility, it is recommended that you be verbose and provide the about:blank URL for now.

+21
May 10 '11 at 7:32 a.m.
source share

It looks like you can also completely exclude src:

http://dev.w3.org/html5/spec/Overview.html#the-iframe-element

If the srcdoc attribute is not set when creating the element, and the src attribute is either not set or is set, but its value cannot be resolved, the browsing context will remain on the initial about: blank page.

+10
Jan 11 '12 at 17:23
source share

You can use about:blank in the src attribute (as previously mentioned by ariel), otherwise it would throw an error when working from a protected page.

A secure https page will generate an error, possibly not secure information on a secure website.

+1
May 10 '11 at 7:34
source share

You can try adding an iframe through Javascript, so you won’t need to have empty text in HTML:

(jQuery example)

 <script type="text/javascript"> $().ready(function() { $("<iframe />").attr("src", "http://www.bbc.co.uk/").appendTo("body"); }); </script> 

Adding an iframe with Javascript allows for graceful degradation - users without Javascript will not see an empty iframe.

+1
May 10 '11 at 12:19
source share

As part of the URI scheme standard, roughly: blank is probably the best option, as it has very good browser support.

about: blank Returns an empty HTML document with the text / html media type and UTF-8 character encoding. This is widely used to load blank pages into view contexts, such as iframes inside HTML, which can then be modified by scripts. Here you can see

+1
Jan 06 '15 at 1:10
source share



All Articles