Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
elementready in 94 bytes
function(
i, // id of element which needs to be selectable until callback can be executed
c, // callback to be executed in the elements scope
t // placeholder for timed out function
){
// create function t
(t = function(e){
// try to select node by ID (i)
(e = document.getElementById(i)) ?
// if we get an result, call callback in its scope
c.call(e) :
// otherwise let it set itself with a 45ms timeout
setTimeout(t,45)
// and execute itself immediately
})()
}
function r(i,c,t){(t=function(e){(e=document.getElementById(i))?c.call(e):setTimeout(t,45)})()}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Alex Kloss <alexhtkloss@web.de>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "elementready",
"description": "Waits until an element selectable by ID is available and then runs a callback in the elements scope.",
"keywords": [
"element",
"ready"
]
}
<!DOCTYPE html>
<title>Element Ready</title>
<div>Expected value: <b>works</b></div>
<script>
var er = function (i,c,t){(t=function(e){(e=document.getElementById(i))?c.call(e):setTimeout(t,45)})()};
// b#ret is not yet available, so lets wait for it.
er('ret', function(){ this.innerHTML = 'works' });
</script>
<div>Actual value: <b id="ret"></b></div>
@atk

This comment has been minimized.

Copy link
Owner Author

atk commented Jun 20, 2011

One could save another 5 bytes by replacing c.call(e) with c(e) - but in this case, the callback would receive the node as argument instead of scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.