Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do `id` attributes need to be unique across iframes?

Tags:

html

iframe

For instance, is this valid?

<!doctype html>
<html>

  <head>
    <title>Some Iframes</title>
  </head>

  <body>
    <iframe id="frame1" src="/html/test-frame.html"></iframe>
    <iframe id="frame2" src="/html/test-frame.html"></iframe>
  </body>

</html>

Where the file test-frame.html has the contents:

<!doctype html>
<html>

  <head>
    <title>Test Iframe</title>
  </head>

  <body>
    <button id="subscribe">Subscribe</button>
  </body>

 </html>

I would like to be able to use the same html and the same scripts for a set of iframes.

like image 650
Mack Avatar asked Apr 10 '14 04:04

Mack


2 Answers

That's acceptable and valid. As each page is self-contained and therefore has its own individual namespace and DOM, there's no harm in using the same ID across iframes as long as it only occurs once within each iframe document.

like image 164
BoltClock Avatar answered Sep 27 '22 23:09

BoltClock


Each page inside of an iframe is totally self-contained and unique.

You can have a page and 4 iframes, and each one can have an element called "#the-element".
Of course, the code that you have there is invalid, but if each loaded its own separate HTML, that would be perfectly acceptable.

like image 45
Norguard Avatar answered Sep 27 '22 22:09

Norguard