简体   繁体   English

如何使用JavaScript抑制IE7 ActiveX消息?

[英]How do you suppress IE7 ActiveX messages using javascript?

I'm trying to decide whether to use a custom ASP.Net Ajax Extender or jQuery to perform a simple web service call. 我试图决定是否使用自定义ASP.Net Ajax Extender或jQuery执行简单的Web服务调用。 The web service method accepts a customer ID and returns the customer name. Web服务方法接受客户ID并返回客户名称。 I'm leaning towards jQuery because of it's simplicity. 由于它的简单性,我倾向于jQuery。 The only problem is that due to my company's IE7 Group Policy settings, the first time jQuery invokes a web service it prompts the user with the following message: 唯一的问题是,由于我公司的IE7组策略设置,jQuery第一次调用Web服务时会提示用户以下消息:

A script is accessing some software (an ActiveX control) on this page which has been marked safe for scripting. 脚本正在访问此页面上的某些软件(ActiveX控件),该软件被标记为可以安全编写脚本。 Do you want to allow this? 您要允许吗?

The Extender does not cause this message to be displayed. 扩展程序不会导致显示此消息。 I'm assuming the ASP.Net Ajax library has some javascript voodoo that suppresses it. 我假设ASP.Net Ajax库具有一些抑制它的JavaScript伏都教。 So my questions is, How do I suppress this message using javascript? 所以我的问题是, 如何使用JavaScript禁止显示此消息?

Here's my aspx markup: 这是我的aspx标记:

<h1>
    Finder Test</h1>
<div>
    <h2>
        Extender</h2>
    Customer ID:
    <asp:TextBox ID="txtCustomerId" runat="server" MaxLength="9" Width="4em" />
    <belCommon:FinderExtender ID="extCustomerId" runat="server" TargetControlID="txtCustomerId"
        ResultLabelID="lblResult" ServicePath="~/Customer.asmx" ServiceMethod="Name" />
    <asp:Label ID="lblResult" runat="server" />
</div>
<div>
    <h2>
        jQuery</h2>
    Customer ID:
    <input id="txtCustomerId2" type="text" maxlength="9" style="width: 4em;" value="0000" />
    <span id="txtCustomerName2"></span>

    <script type="text/javascript">
        $(document).ready(function()
        {
            $("#txtCustomerId2").change(
            function()
            {
                updateCustomerDescription(this.value, "txtCustomerName2");
            }).change();
        });

        function updateCustomerDescription(id, descriptionControlId)
        {
            // if we don't have a value, then don't bother calling the web service
            if (id == null || id.length == 0)
            {
                $("#" + descriptionControlId).text("");
                return;
            }

            jsonAjax("customer.asmx/Name", "{'id':'" + id + "'}", true,
                function(result)
                {
                    var name = result.d == null ? "" : result.d;
                    $("#" + descriptionControlId).text(name);
                }, null);
        }

        function jsonAjax(url, data, async, onSuccess, onFailed)
        {
            $.ajax({
                async: async,
                type: "POST",
                url: url,
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: onSuccess,
                error: onFailed
            });
        }
    </script>
</div>

[Update] [更新]

I'm assuming that the ActiveX control referenced in the message is XMLHttpRequest. 我假设消息中引用的ActiveX控件是XMLHttpRequest。 I'm also assuming that the internals of jQuery and ASP.Net Ajax both use it for IE7. 我还假设jQuery和ASP.Net Ajax的内部都将其用于IE7。

[Update] [更新]

The difference appears to be in how ASP.Net Ajax and jQuery construct an instance of XMLHttpRequest. 区别似乎在于ASP.Net Ajax和jQuery如何构造XMLHttpRequest的实例。

ASP.Net Ajax (thanks @Jesse Dearing): ASP.Net Ajax(感谢@Jesse Dearing):

 window.XMLHttpRequest = function window$XMLHttpRequest() {
 var progIDs = [ 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP' ];
 for (var i = 0, l = progIDs.length; i < l; i++) {
  try {
    return new ActiveXObject(progIDs[i]);
  }
  catch (ex) { }
  }
     return null;
  }
}

jQuery 1.3.2: jQuery 1.3.2:

// Create the request object; Microsoft failed to properly
// implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
// This function can be overriden by calling jQuery.ajaxSetup
xhr:function(){
    return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}

This doesn't sound right to me. 对我来说这听起来不对。 If there is a security policy in place, you should not be able to override it with javascript. 如果有适当的安全政策,则您将无法使用javascript覆盖它。

I would double check the two pages and see if they both really access the same ActiveX control. 我会仔细检查这两个页面,看看它们是否真的访问相同的ActiveX控件。 I assume that the pages are coming from the same host (so there's no issue of different WinInet trust zones). 我假设页面来自同一主机(因此不会出现不同WinInet信任区的问题)。

I'm working on an ASP.NET AJAX app so I checked the source of it and I found this in the ASP.NET AJAX code: 我正在使用ASP.NET AJAX应用程序,因此我检查了源代码,并在ASP.NET AJAX代码中找到了它:

 window.XMLHttpRequest = function window$XMLHttpRequest() {
 var progIDs = [ 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP' ];
 for (var i = 0, l = progIDs.length; i < l; i++) {
  try {
    return new ActiveXObject(progIDs[i]);
  }
  catch (ex) { }
  }
     return null;
  }
}

I hope that helps some. 希望对您有所帮助。

I solved this by overriding jQuery's xhr function as so: 我通过重写jQuery的xhr函数来解决此问题:

function overrideJqueryXhr()
{
    $.ajaxSetup({
        xhr: function()
        {
            if (window.XMLHttpRequest)
            {
                return new XMLHttpRequest();
            }
            else
            {
                var progIDs = ['Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];

                for (var i = 0; i < progIDs.length; i++)
                {
                    try
                    {
                        var xmlHttp = new ActiveXObject(progIDs[i]);
                        return xmlHttp;
                    }
                    catch (ex)
                    {
                    }
                }

                return null;
            }
        }
    });
}

This function tells jQuery to create an instance of the XMLHttpRequest class for non-IE browsers, and then creates an ActiveX object the MS Ajax way for IE. 此函数告诉jQuery为非IE浏览器创建XMLHttpRequest类的实例,然后以MS Ajax方式为IE创建ActiveX对象。 It tries the latest version first, Msxml2.XMLHTTP.3.0, then Msxml2.XMLHTTP and finally Microsoft.XMLHTTP. 它首先尝试最新版本,即Msxml2.XMLHTTP.3.0,然后是Msxml2.XMLHTTP,最后是Microsoft.XMLHTTP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM