2

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.

Max Truxa
  • 3,308
  • 25
  • 38
  • 2
    How do you put "plain Javascript" into an HTML document...? – deceze Jul 29 '14 at 16:35
  • @deceze OFC there are script tags, but the extra tag for every single event is driving me crazy. And it's completely impractical, as I'm wrapping the creation and interaction with the control in an own class. Furthermore, if you have multiple instances of the control you have to add a separate script tag for every event you are interested in on every single instance. – Max Truxa Jul 29 '14 at 17:13
  • JScript and JavaScript are not the same. I don't believe you can get away with mixing languages. – Claies Jul 29 '14 at 17:20
  • 2
    As you asking if it's possible to use standard JavaScript event handlers to control the WMP plug-in (ActiveX?) rather than that funky JScript specific syntax based on HTML attributes? – Álvaro González Jul 29 '14 at 17:25
  • For the most part they are the same, JScript just has a few syntax features not present in JavaScript. See [this SO question](http://stackoverflow.com/questions/135203/whats-the-difference-between-javascript-and-jscript). Besides this script-tag-event-handling-thing I'm writing JavaScript compliant code. – Max Truxa Jul 29 '14 at 17:28
  • @Álvaro G. Vicario That would be the perfect solution, but I couldn't get that to work. There are some standard event handler present on the control (like `onvolumechange`) but these are not being triggered. Some sort of intermediate code that dispatches to manually create JavaScript events would be just fine. – Max Truxa Jul 29 '14 at 17:30
  • 1
    in your case, they are not equivalent. Microsoft does not provide a JavaScript API for controlling the WMP ActiveX control. – Claies Jul 29 '14 at 17:32

1 Answers1

3

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.

Helen
  • 87,344
  • 17
  • 243
  • 314