【问题标题】:How can I display some text when an embedded object fails to display?当嵌入对象无法显示时,如何显示一些文本?
【发布时间】:2009-03-31 19:55:16
【问题描述】:

我们有一些看起来像这样的 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,这只是一个临时解决方案(

【问题讨论】:

    标签: .net javascript html user-controls embedded-object


    【解决方案1】:

    您可以将任何内联元素放在对象标签内,如果加载失败,它们将呈现给用户。

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

    MSDN: OBJECT Element | object Object

    想添加一些关于奇怪的 IE 行为的注释。 (IE

    <!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

    的返回值
    byId = null
    byTags = 2
    access = false
    supported = true
    

    W3C 兼容

    的返回值
    byId = [object HTMLObjectElement]
    byTags = 3
    access = false
    no-access = false
    supported = false
    

    Try for yourself

    【讨论】:

    • 您可能想要清理您的示例,尤其是结束标记。而且我不认为它仅限于内联元素。为正确答案 +1。
    • 感谢拼写错误。我提到内联是因为我隐约记得当我尝试使用一些块时遇到 DOM 问题(在 IE 中!)
    • 感谢 L 先生。我仍然只得到这篇文章中提到的“彩色图标”(blog.jonschneider.com/2007/04/windows-forms-controls-in-ie.html)。我的临时 Internet 文件中也出现了融合绑定错误。
    • 谢谢!我在能够加载控件的机器和不能加载控件的机器上进行了尝试。在这两种情况下,我都得到了 byID 的 [object],byTags 的 1,以及显示 MyUserControl = false 的行。我会继续工作,但会给你一个答案。我怀疑我做错了什么。
    • 这可能不是你做错了,但听起来控件在它的初始化代码中导致了一个异常,这并没有完全扩散到 DOM/JS 范围内。因此,它需要在您的 Control 代码中的更高级别进行调试。
    【解决方案2】:

    根据您的情况添加另一个更具体的答案。

    将您的 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>');
    

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2022-10-12
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多