Working with DOM events

I've set up a listener function to listen for the 'qt=ended' DOM event that the Quick Time player sends out. The code I have to register the listener is:

function register_EndListener () {
		var obj = document.getElementById('Poets');
        if ( !obj )
            obj = document.getElementById('embedPoets');
        if ( obj ) {
        	if ( document.addEventListener )
            	obj.addEventListener('qt_ended', songFinished, false);
        	else
            // IE
	            obj.attachEvent('on' + 'qt_ended', songFinished);
    	}
}

Open in new window


This runs as part of window.onload.

This works but only after I click back on the page that has the above JavaScript.  It seems that somehow the page lost focus and the event will only be sent to a page that has focus. Actually, when the song starts, a page pops up showing the song words, and after the song finishes I can get the 'qt_ended' event delivered if I click in either the popup window or the main window behind the popup.  What I want is for the event to to be delivered to its handler without anyone having to click somewhere.

Thanks for any suggestions.

Steve
stevaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
was the 'Poets' element loaded before document load or after document load?
stevaAuthor Commented:
Not sure.  The body contains a call to Apple's QT_WriteOBJECT Javascript routine,  which modifies the DOM to put in the proper <object>/<embed> tags given the parameters specified:  

 
<div id="player">
		<script language="javascript" type="text/javascript">
    
        QT_WriteOBJECT('GoGentle_1_256kbs.mp3' , '100', '15', '', 'AUTOPLAY', 'True', 'CONTROLLER', 'True', 'postdomevents', 'True', 'PLUGINSPAGE', 'http://www.apple.com/quicktime/', 
            'EnableJavaScript', 'True', 'emb#NAME' , 'Poets' , 'obj#id' , 'Poets', 'embed#id', 'embedPoets');

		</script>
	</div>

Open in new window


I don't know if the JavaScript runs when it's encountered or waits for the whole document to load, or not.
Gurvinder Pal SinghCommented:
this doesn't necessarily runs after the document has loaded.
May be, you can run a timer event periodically to check if this is loaded and then register the event handler
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

stevaAuthor Commented:
So you think that maybe the Poets element doesn't exist yet when I register the handler, so the handler doesn't get registered?  But then why does the event come through and get handled when I click in the popup or the main widow?
Gurvinder Pal SinghCommented:
<<So you think that maybe the Poets element doesn't exist yet when I register the handler, so the handler doesn't get registered? >>
Yes, it seems to be the case.


<<But then why does the event come through and get handled when I click in the popup or the main widow?>>
Because by the time you click, it poet element is ready
stevaAuthor Commented:
So the registration code suspends itself until the click to bring focus back makes it retry?

I could put the actual object/embed code in the HTML, instead of letting QT_WriteOBJECT() put it in, and then we'd know that Poets was there when the registration code ran at document ready.

Gurvinder Pal SinghCommented:
<<So the registration code suspends itself until the click to bring focus back makes it retry?>>
No, it doesn't wait for the document ready to happen before embedding the code. It just runs as a parallel thread.
stevaAuthor Commented:
I put the full object/embed code into the HTML, instead of letting QT_WriteOBJECT() do it, but the results are the same.  The qt_ended event doesn't get delivered until I click in the lyrics popup or the background. With the Poets element certainly existing now when the registration function runs at document ready, it seems certain that the registration is occurring correctly, yet the event still isn't delivered after it fires at the end of the song,  until I click something.

stevaAuthor Commented:
I solved t his myself by purchasing and using the Longtail JW player. It has all the events I needed and they were easy to work with to get the music synchronized with the words..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stevaAuthor Commented:
Sorry for not giving out the points but I had to solve this myself.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.