我们有一些HTML,看起来像这样:

<form id="MyUserControl" runat="server" method="post">
   <script language="javascript" src="javascript.js"></script>

JavaScript文件包含这样的:

document.write('<object id="MyUserControl" ');
document.write('classid="MyUserControl.dll#MyNamespace.MyUserControl"');
document.write('height="611" width="951" >');
document.write('<param name="Parm1" value="' + parm1 + '" />');
document.write('</object>');

基本上,我想能够显示一些文本或代替离开待机文本,如果该对象不正常加载。

原因这一切的是,我们正在试图从FoxPro中一个旧的应用程序迁移到.NET,而它仍然在使用。这是一个相当大的应用程序,我们在一个时间转换的画面。我们正在建设的.Net用户控件和它们显示在浏览器对象的FoxPro的内部。上述HTML和js允许我们嵌入在网页中的的WinForms用户控制。

如果一个特定的用户没有为我们设置了应用服务器代码访问组,用户控件不呈现,屏幕只显示在左上角,看起来像一些试图加载的小图标。它从来不负载,但我想通知用户电子邮件到我们的团队得到他们的访问理顺。

我尝试使用待机和alt,但既没有表现出文本。我们所有的用户都IE7,这只是一个临时的解决方案(<6个月)。

有帮助吗?

解决方案

可以把对象标记内任何内嵌元件,并且,如果它未能加载,它们将被呈现给用户。

<object>
  This object failed to load,
  try to install <a href="http://plugin-site/">here</a>
</object>

MSDN:对象元素|对象的对象

想添加约奇数IE行为一些注释。 (IE <8) 当使用上面的代码中失败的内容是要显示,您将无法访问在DOM对象。所以,你可能想保留它空,并做一些额外的代码来确定负载成功。

<!DOCTYPE html>
<html>
<head>
  <title>so-objects</title>
  <script type="text/javascript">

    function add(text) {
      document.body.appendChild(document.createElement("div"))
                   .appendChild(document.createTextNode(text));
    };

    function each(o, f, c) {
      for (var i=0, l=o.length; i<l; i++) f.call(c, o[i], i, o);
    };

    onload = function() {

      // IE returns null and has mysteriously removed the object from DOM
      add("byId = "+ document.getElementById("no-access") );

      // IE returns 2 instead of 3
      add("byTags = "+ document.getElementsByTagName("object").length );

      // verify successful load if the object property is available
      each(document.getElementsByTagName("object"), function(node) {
        add(node.id +" = "+ !!node.object);
      });

    };

  </script>
  <style type="text/css">
    object, span { position: absolute; left:-10000px; }
  </style>
</head>
<body>

  <object id="access">
  </object>

  <object id="no-access">
    <span>failed</span>
  </object>

  <object id="supported"
     classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6">
  </object>

</body>
</html>

发回值的 IE <8

byId = null
byTags = 2
access = false
supported = true

有返回值的 W3C标准

byId = [object HTMLObjectElement]
byTags = 3
access = false
no-access = false
supported = false

自己试着

其他提示

添加更多具体到你的情况另一个答案。

改变你的JavaScript来,这可能产生所需的结果(如果有被施加到页面此对象的仅一个实例)。

function onchange(o) {
  if (o.readyState == 4) {
    if (o.object == null) {
      alert("UserControl is not installed, please email your administrator.");
      // or, toggle display of a message dialog in HTML
      //document.getElementById("not-installed").style.display = "block";
    }
  }
};

document.write('<object id="MyUserControl"');
document.write(' classid="MyUserControl.dll#MyNamespace.MyUserControl"');
document.write(' height="611" width="951" onreadystatechange="onchange(this)">');
document.write(' <param name="Parm1" value="' + parm1 + '" />');
document.write('</object>');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top