Question

I'm embedding a Windows Media Player control in an HTML page.

<object width="720"
        height="480"
        id="Player"
        classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6">
    <!-- <param> tags left out -->
</object>

To subscribe to events from the control (e.g. PlayStateChange(newState)), I have to add a <script> tag, like the following:

<script language="JScript" for="Player" event="PlayStateChange(newState)">
    var newStateText = document.createTextNode('' + newState);
    var playStateContainer = document.getElementById('PlayState');
    while (playStateContainer.firstChild) {
        playStateContainer.removeChild(playStateContainer.firstChild);
    }
    playStateContainer.appendChild(newStateText);
</script>

Isn't there a way to subscribe to these event through plain JavaScript (without the extra <script> tag for every event I'm interested in on every single instance of the control)?

With 5 instances of the control and 5 events I want too subscribe to, this would be 25 script tags. Furthermore, the control is wrapped in some library code in an external JavaScript file. The event handling code should be there too, and not in the HTML of the client application.

Was it helpful?

Solution

According to this MSDN article (see sections Naming Conventions and Automagic), you can write all your event handlers inside the same <script> tag, provided that you name the functions objectname::eventname or objectname.eventname:

<script language="JScript">
function Player::playStateChange(newState) {
  ...
}

function Player.currentPlaylistChange(change) {
  ...
}
</script>

I didn't try this though. And of course, this is all IE-specific.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top