Search code examples
javascriptnpapifirebreath

JsApi sometimes gets broken


We're using the JsApin for two way communication between the plugin and the page js. Sometimes this object just stops working.

Here's what we're doing:

<object type="...">
    ...
    <param name="onload" value="pluginloaded" />
    ...
</object>

var myObj = {
    element: ..., // reference to the dom element for the object tag
    ...
}

function pluginloaded(jsapi) {
    myObj.jsapi = jsapi;
}

As you can see we keep a reference to both the DOM object element and the JsApi object that is being passed to the onload method.
When there's a try to execute a method on the plugin (or a property) it will first try myObj.jsapi.method() and if that failed then 'myObj.element.method()`, and if that fails then that will be reported.

Statistics show that it doesn't happen very frequently, but it does indeed happen, though I have no idea how to reproduce it, just sometimes happens.

Any idea what might cause this object to be unavailable? from the js perspective the jsapi object is undefined, and the object element just doesn't have the method/properties which are exposed from the plugin.

Thanks.


Solution

  • Without spending more time looking at your actual project it's hard to say for sure, but this sounds to me like you're actually unloading and/or reloading your plugin. The most common thing that causes this is when you move it around in the DOM -- for example, if you use javascript to create the object tag, and you set the .type before you put it in the DOM:

    var obj = Document.create("object");
    obj.type = "application/x-mytype";
    
    someElement.appendChild(obj);
    

    This seems like a good idea, but the browser actually will partially destroy the plugin object when you do this. Similarly, if you set css display: none or overflow: {anything here} on the plugin object or any of its parents it can cause this.

    Anyway, however it is happening I bet you are unloading the plugin, thus invalidating the jsapi object you grabbed.