javascripthtmldynamic-web-twain

Object tag added by innerHTML always null onload


I am creating an object tag onload, and after creating it trying to access the object as follows:

function loadFile() {



        alert("loading file");
        gform.MOJWebTwain.HTTPPort = 9080;
        gform.MOJWebTwain.IfSSL = false;
        var result = gform.MOJWebTwain.HTTPDownload('myServer', existingDocumentPath);
        alert("result: "+result);
        var errorString = gform.MOJWebTwain.ErrorString;
        var errorCode = gform.MOJWebTwain.ErrorCode;
        if (!result) {
            alert("errorString: " + errorString);
            alert("errorCode " + errorCode);
        }
        document.body.style.opacity="100";

}

window.onload = function () {
    var DW_DWTContainerID = "gform:scan_area";
    var DW_FULLCLASSID = "123456-abcdef-12345-abcdef-123456"; // fixed
    var DW_PROCLASSID = "3d25aba1-abcdef-12345-abcdef-123456";  // the one in lpk file

    var DW_VersionCode = "6,3,1,0";
    var DW_InWindowsX86;
    var objString = "";

    var DW_ObjectName="MOJWebTwain"
    var DW_Width = 400;
    var DW_Height = 400;
    var DW_CABX86Path = "DynamSoft/DynamicWebTWAIN.cab";
    var DW_CABX64Path = "DynamSoft/DynamicWebTWAINx64.cab";
    var LPK_Path = "DynamSoft/DynamicWebTwain.lpk";
    ua = (navigator.userAgent.toLowerCase());

    objString += "<object id='" + DW_ObjectName + "' "+"width='" + DW_Width +" ' "+"height='" + DW_Height+"' ";

    if (DW_InWindowsX86) objString += "codebase='" + DW_CABX86Path + "#version=" + DW_VersionCode + "' ";
    else objString += "codebase='" + DW_CABX64Path + "#version=" + DW_VersionCode + "' ";

    objString += " classid='clsid:" + DW_FULLCLASSID + "' viewastext>";

    //alert("objString: "+objString);

    objString += " <param name='_cx' value='847'/>";
    objString += " <param name='_cy' value='847'/>";
    objString += " <param name='JpgQuality' value='30'/>";
    objString += " <param name='Manufacturer' value='DynamSoft Corporation'/>";
    objString += " <param name='ProductFamily' value='Dynamic Web TWAIN'/>";
    objString += " <param name='ProductName' value='Dynamic Web TWAIN'/>";
    objString += " <param name='VersionInfo' value='Dynamic Web TWAIN 6,3,1,0'/>";
    objString += " <param name='TransferMode' value='0'/>";
    objString += " <param name='BorderStyle' value='0'/>";
    objString += " <param name='FTPUserName' value=''/>";
    objString += " <param name='FTPPassword' value=''/>";
    objString += " <param name='FTPPort' value='21'/>";
    objString += " <param name='HTTPUserName' value=''/>";
    objString += " <param name='HTTPPassword' value=''/>";
    objString += " <param name='HTTPPort' value='9080' />";
    objString += " <param name='ProxyServer' value=''/>";
    objString += " <param name='IfDisableSourceAfterAcquire' value='0'/>";
    objString += " <param name='IfShowUI' value='0'/>";
    objString += " <param name='IfModalUI' value='-1'/>";
    objString += " <param name='IfTiffMultiPage' value='-1'/>";
    objString += " <param name='IfThrowException' value='0'/>";
    objString += " <param name='MaxImagesInBuffer' value='99'/>";
    objString += " <param name='TIFFCompressionType' value='0'/>";
    objString += " <param name='IfFitWindow' value='-1' />";
    objString += " <param name='IfSSL' value='0' />";
    objString += " </object>";

    var obj2String = "<object "
        +"classid='clsid:"+DW_PROCLASSID+"'"+">"
        +"<param name='LPKPath' value='"+LPK_Path+"'"+"/>";
        +"</object>";

    //alert("obj2String "+obj2String);

    var obj2String = "<object classid='clsid:"+DW_PROCLASSID+"'>"
        +"<param name='LPKPath' value='"+LPK_Path+"'"+"/>";
        +"</object>";

    var objects_string=obj2String +" "+objString;

    DWTContainer = document.getElementById(DW_DWTContainerID);                          
    DWTContainer.innerHTML = objects_string;

    loadFile();
}

HTML is as follows:

<form id="gform">
  <div id=gform:scan_area"">

   </div>
</form>

But I always get that the object I created is null in this line:

gform.MOJWebTwain.HTTPPort = 9080;

If the object was added statically in the page (i.e. not through onload), I don't get this exception.


Solution

  • Fixed By using setTimeout as follows:

    setTimeout(function(){
            DWTContainer = document.getElementById(DW_DWTContainerID);
            alert("DWTContainer: "+DWTContainer);
            DWTContainer.innerHTML = objects_string;
            loadFile(editWebScan,existingDocumentPath);
        }, 500);