innerHTML添加的对象标记始终为null onload

时间:2013-06-10 10:26:42

标签: javascript html dynamic-web-twain

我正在创建一个对象标记onload,并在创建它后尝试访问该对象,如下所示:

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 如下:

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

   </div>
</form>

但我始终认为我在这一行创建的对象是null

gform.MOJWebTwain.HTTPPort = 9080;

如果对象是在页面中静态添加的(即不是通过onload),我不会得到这个例外。

7 个答案:

答案 0 :(得分:2)

您的HTML部分似乎是错误,如下所示

<form id="gform">
  <div id=gform:scan_area""><!--quote error-->

   </div>
</form>

检查一下它可以给你想要的结果

<script>
    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;
        console.log(objects_string);

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

        loadFile();
    }
</script>

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

   </div>
</form>

答案 1 :(得分:1)

调用其方法/属性时,请确保对象已准备就绪。您可以通过检查属性的值来验证对象状态。以下是一个简单的示例:

window.onload = function (){
var DW_Seed;   // The seed used to detect the control.
DW_Seed = setInterval(DW_ControlDetect, 500);
}
function DW_ControlDetect() {
// If the ErrorCode is 0, it means the control is fully loaded.
if (DWObject.ErrorCode == 0) {
   // loadFile()...
}
}

答案 2 :(得分:1)

修复了使用setTimeout,如下所示:

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

答案 3 :(得分:0)

它必须是var gform范围的问题。宣布在哪里?我在你的代码中没有看到它。

如果在其他脚本中创建了该var,请确保之前加载了该脚本。还要确保在全局范围内创建gform var。要确保它位于全球范围内,您可以将其附加到window,(window.gform = value

答案 4 :(得分:0)

我删除了文件加载功能,因为它似乎无关紧要,你的代码的语法不正确,我纠正了它,现在它正在工作,我可以从文档中选择对象..检查出来:

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;

    var DWTContainer = document.getElementById(DW_DWTContainerID);                          
    DWTContainer.innerHTML = objects_string;
    console.log(document.getElementsByName('FTPPassword')[0]);
    //loadFile();
};

<强> DEMO

关于gform对象,你还没有为该变量分配任何对象!所以是的,它将是空的或未声明的。

注意:我不禁注意到您正在使用警报进行测试,这会让您在循环中运行时感到头痛,更好地使用console.log(更安全和更快),如此代码所示,以及检查浏览器的控制台以查看检查模式中的结果

答案 5 :(得分:0)

@Mahmoud尝试以下代码,我无法使用Jsfiddle检查您的代码(有没有办法提供访问服务器的凭据?要测试?

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;

setInterval(function(){loadFile()},1000); // this will delay it by a second just in case some other instructions should be done ...
} 

答案 6 :(得分:0)

根据Rachel的建议,您可以尝试以下方法:

function loadFile() {
    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());
    if (ua.indexOf("win64") != -1 && ua.indexOf("x64") != -1)
        DW_InWindowsX86 = false;
    else
        DW_InWindowsX86 = true;
    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>";
    objString += " </object>"; 

    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;
    DW_Seed;   // The seed used to detect the control.
    DW_Seed = setInterval(DW_ControlDetect, 500);

}
function DW_ControlDetect() {
    if (gform.MOJWebTwain.ErrorCode == 0) {
        loadFile();
    }
}