Skip to content

Commit

Permalink
Fix DOMContentLoaded with async tag
Browse files Browse the repository at this point in the history
  • Loading branch information
craigfrancis committed Mar 22, 2016
1 parent 316dee5 commit 2914157
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 54 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions additional/height-animation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h2>Current solution (jQuery)</h2>
</div>
</div>

<script src="../a/jquery-1.12.2.min.js"></script>
<script src="../../a/jquery-1.12.2.min.js"></script>
<script>

$(function() {
Expand Down Expand Up @@ -144,7 +144,7 @@ <h2>Proposed solution</h2>


<script src="./toggle.js" async="async"></script>
<script src="../a/analytics.js" async="async"></script>
<script src="../../a/analytics.js" async="async"></script>

</body>
</html>
88 changes: 43 additions & 45 deletions additional/height-animation/toggle.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,70 @@

(function(document, window, undefined) {
;(function(document, window, undefined) {

'use strict';
'use strict';

if (!document.addEventListener || !('classList' in document.createElement('span'))) { // classList = IE11
return;
}

function widget_toggle(e) {

var widget_ref = document.getElementById(this.getAttribute('aria-controls'));
if (!document.addEventListener || !('classList' in document.createElement('span'))) { // classList = IE11
return;
}

if (widget_ref.getAttribute('aria-hidden') == 'true') {
function widget_toggle(e) {

this.setAttribute('aria-expanded', 'true');
this.textContent = this.getAttribute('data-toggle-hidden');
var widget_ref = document.getElementById(this.getAttribute('aria-controls'));

widget_ref.setAttribute('aria-hidden', 'false');
widget_ref.focus();
if (widget_ref.getAttribute('aria-hidden') == 'true') {

} else {
this.setAttribute('aria-expanded', 'true');
this.textContent = this.getAttribute('data-toggle-hidden');

this.setAttribute('aria-expanded', 'false');
this.textContent = this.getAttribute('data-toggle-shown');
widget_ref.setAttribute('aria-hidden', 'false');
widget_ref.focus();

widget_ref.setAttribute('aria-hidden', 'true');
} else {

}
this.setAttribute('aria-expanded', 'false');
this.textContent = this.getAttribute('data-toggle-shown');

e.preventDefault();
widget_ref.setAttribute('aria-hidden', 'true');

}

function init() {
e.preventDefault();

var links = document.querySelectorAll('a[data-toggle-hidden]'),
widget_ref_id,
widget_ref;
}

for (var k = (links.length - 1); k >= 0; k--) {
function init() {

widget_ref_id = links[k].getAttribute('href').substr(1);
widget_ref = document.getElementById(widget_ref_id);
var links = document.querySelectorAll('a[data-toggle-hidden]'),
widget_ref_id,
widget_ref;

if (widget_ref) {
for (var k = (links.length - 1); k >= 0; k--) {

widget_ref.setAttribute('aria-hidden', 'true');
widget_ref.setAttribute('tabindex', '-1');
widget_ref.style.outline = 'none';
widget_ref_id = links[k].getAttribute('href').substr(1);
widget_ref = document.getElementById(widget_ref_id);

links[k].setAttribute('data-toggle-shown', links[k].textContent);
links[k].setAttribute('role', 'button');
links[k].setAttribute('aria-expanded', 'false');
links[k].setAttribute('aria-controls', widget_ref_id);
links[k].addEventListener('click', widget_toggle);
if (widget_ref) {

}
widget_ref.setAttribute('aria-hidden', 'true');
widget_ref.setAttribute('tabindex', '-1');
widget_ref.style.outline = 'none';

links[k].setAttribute('data-toggle-shown', links[k].textContent);
links[k].setAttribute('role', 'button');
links[k].setAttribute('aria-expanded', 'false');
links[k].setAttribute('aria-controls', widget_ref_id);
links[k].addEventListener('click', widget_toggle);

}

}

if (document.readyState !== 'loading') {
console.log('a');
window.setTimeout(init); // Handle asynchronously
} else {
console.log('b');
document.addEventListener('DOMContentLoaded', init);
}
}

if (document.readyState !== 'loading') {
window.setTimeout(init); // Handle asynchronously
} else {
document.addEventListener('DOMContentLoaded', init);
}

})(document, window);
})(document, window);
4 changes: 2 additions & 2 deletions additional/textarea/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h2>Current solution (jQuery)</h2>

<textarea id="textarea_b" rows="4" cols="30">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>

<script src="../a/jquery-1.12.2.min.js"></script>
<script src="../../a/jquery-1.12.2.min.js"></script>
<script src="./jquery.autoresize.js"></script>
<script>

Expand Down Expand Up @@ -87,7 +87,7 @@ <h2>Proposed solution</h2>
</div>


<script src="../a/analytics.js" async="async"></script>
<script src="../../a/analytics.js" async="async"></script>

</body>
</html>
4 changes: 3 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
<h1>IFrame Height</h1>
<iframe src="./framed.html" id="iframe"></iframe>

<!-- <script src="./size-same-origin.js"></script> -->
<!-- <script src="./size-cross-origin-parent.js"></script> -->

<script src="./size-same-origin.js" async="async"></script>
<script src="../a/analytics.js" async="async"></script>

</body>
</html>
27 changes: 23 additions & 4 deletions example/size-same-origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,33 @@
return;
}

function init() {
var iframe;

function init_iframe() {
var height = Math.max(iframe.contentWindow.document.body.scrollHeight, iframe.contentWindow.document.body.offsetHeight, iframe.contentWindow.document.documentElement.clientHeight, iframe.contentWindow.document.documentElement.scrollHeight, iframe.contentWindow.document.documentElement.offsetHeight);
iframe.style.height = height + 'px';
}

var iframe = document.getElementById('iframe');
if (iframe) {
iframe.addEventListener('load', init);
function init() {
iframe = document.getElementById('iframe');
if (iframe) {

iframe.addEventListener('load', init_iframe);

try {
if (iframe.contentWindow.document.readyState !== 'loading') {
window.setTimeout(init_iframe);
}
} catch (e) {
}

}
}

if (document.readyState !== 'loading') {
window.setTimeout(init); // Handle asynchronously
} else {
document.addEventListener('DOMContentLoaded', init);
}

})(document, window);

0 comments on commit 2914157

Please sign in to comment.