473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Waiting for an element to be ready in js

I want to add some event handlers to elements on a page, but most of
the elements are generated by ajax and aren't created until the ajax
response finishes. Is there a better way than what I have below to do
this?
function waitForA(){
var ele = document.getElementById('meh');
if(!ele){
window.setTimeout(waitForA, 100);
}
else{
ele.addEventListener('mouseup', ...);
}
}

function waitForB(){
var ele = document.getElementById('bah');
if(!ele){
window.setTimeout(waitForB, 100);
}
else{
ele.addEventListener('mouseup', ...);
}
}

function waitForC(){
var ele = document.getElementById('sneh');
if(!ele){
window.setTimeout(waitForC, 100);
}
else{
ele.addEventListener('mouseup', ...);
}
}
I would really like to just be able to do something like this :

gottaWait('elementID').addEventlistener('...

so that I don't have to write a "wait" function for each element.
Jun 27 '08 #1
3 2449
Yansky wrote:
I want to add some event handlers to elements on a page, but most of
the elements are generated by ajax and aren't created until the ajax
response finishes. Is there a better way than what I have below to do
this?
[...]
Most definitely. Add the event listener(s) when "the "ajax response
finishes", on readyState=4 of the request, instead, directly after the
element was created. If your XHR library is sophisticated enough, you
just have to adapt your *XHR event listener* slightly.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #2
On Fri, 02 May 2008 04:47:51 +0200, Yansky <th*********@gmail.comwrote:
I want to add some event handlers to elements on a page, but most of
the elements are generated by ajax and aren't created until the ajax
response finishes. Is there a better way than what I have below to do
this?
I'd find a way to do let it be run just once when the response is there,
using the state changes...
function waitForA(){
var ele = document.getElementById('meh');
if(!ele){
window.setTimeout(waitForA, 100);
}
else{
ele.addEventListener('mouseup', ...);
}
}

function waitForB(){
var ele = document.getElementById('bah');
if(!ele){
window.setTimeout(waitForB, 100);
}
else{
ele.addEventListener('mouseup', ...);
}
}

function waitForC(){
var ele = document.getElementById('sneh');
if(!ele){
window.setTimeout(waitForC, 100);
}
else{
ele.addEventListener('mouseup', ...);
}
}
I would really like to just be able to do something like this :

gottaWait('elementID').addEventlistener('...

so that I don't have to write a "wait" function for each element.
something like this perhaps?:
function delayedEvent(strId,strEventType,eventAction){
var ele = document.getElementById(strId);
if(ele){
ele.addEventListener(strEvent,eventAction);
} else {
window.setTimeOut(delayedEvent,strId,strEventType, eventAction);
}
}
--
Rik Wasmus
Jun 27 '08 #3
Rik Wasmus wrote:
On Fri, 02 May 2008 04:47:51 +0200, Yansky <th*********@gmail.comwrote:
>I want to add some event handlers to elements on a page, but most of
the elements are generated by ajax and aren't created until the ajax
response finishes. Is there a better way than what I have below to do
this?

I'd find a way to do let it be run just once when the response is there,
using the state changes...
Most definitely.
>[...]
I would really like to just be able to do something like this :

gottaWait('elementID').addEventlistener('...

so that I don't have to write a "wait" function for each element.

something like this perhaps?:
Not very much like this. (Maybe you should get the basics right before you
try to make suggestions?)
function delayedEvent(strId, strEventType, eventAction){
function delayedEvent(sID, sEventType, fListener)
{
var ele = document.getElementById(strId);
var ele = document.getElementById(sID);
if(ele){
if (typeof fListener == "function")
{
if (ele)
{
ele.addEventListener(strEvent,eventAction);
ele.addEventListener(sEventType, fListener, false);

But it's still incompatible and (therefore) error-prone anyway: The MSHTML
DOM does not implement W3C DOM Level 2+ Events, you'll need a wrapper like
_addEventListener() in http://PointedEars.de/scripts/dhtml.js
} else {
window.setTimeOut(delayedEvent,strId,strEventType, eventAction);
// add feature test here
window.setTimeout(
function() {
delayedEvent(sID, sEventType, fListener);
},
250);
}
}
}

But, as we established before, this could only be a dirty hack, eating up
the client system's resources, and not a substitute for proper coding the
XHR event listener at all.

BTW, do not use tabs for indentation; use spaces which are uniformly displayed.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Josh | last post by:
Hi - A couple days ago I posted asking for help on how to download a pushed file. I am trying to write a script to download a bunch of links from a page that takes a while to load. I managed to...
1
by: Brian Kitt | last post by:
I have a web app, where the user can enter search criteria, then click 'submit'. The search takes a few seconds. My customer wants a 'waiting' screen to come up with an animated Gif, then replace...
3
by: Elhurzen | last post by:
X-No-Archive: Yes >From what I understand, if multiple threads are waiting on a ManualResetEvent after calling WaitOne(), only one thread is guaranteed to be signaled when Set() is called on that...
12
by: Raymond Lewallen | last post by:
How to wait for a process to stop completion is my goal. Obviously, the looping while waiting for the HasExited property is not a solution.. but thats the best I can come up off the top of my...
16
by: Bruce Wood | last post by:
Maybe it's just late in my day, but I'm reading Jon's article on threading, in particular how to use Monitor.Wait() and Monitor.Pulse(), and there's something that's not sinking in. The code in...
5
by: Hansen | last post by:
Hi! I have a problem with an object not yet being initialized when I try to access it. Hence I would like to wait for the object to be initialized. I have the following code: try {...
1
by: arun.hallan | last post by:
I need the following functionality. public Object RunValuation { run process that writes to database check the database every second to check if results are ready when results are ready,...
2
by: strauchdieb | last post by:
hi, i have a non-visible iframe on my page. in that iframe i load data in a htmltable. if the data is ready, i want to take this table out of the iframe and insert it in the parent. as soon as...
4
by: Jono | last post by:
Hi Everyone, As it says in the title, I'm looking for a way to display a page while long running operations are performed on the server. Ideally, I'd like some way to push the current request...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.