IE文档模式更改无效

时间:2013-03-21 17:30:47

标签: asp.net-mvc internet-explorer knockout.js signalr

在我们的组织中,我们有一个策略应用于所有用户,以便IE9进入IE7渲染模式,以便与某些第三方应用程序兼容。我正在开发一个使用SignalR和KnockoutJS的新应用程序,我正在使用

强制IE进入IE9模式

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

但出于某种原因,IE不会切换?如果我进入开发人员工具,它表明应用程序以IE9模式呈现,但实际上并不正常。如果我在没有打开开发人员工具的情况下刷新IE,它就会被破坏,但如果我用开发者工具刷新IE,那么应用程序就会开始工手动切换到IE9渲染模式也可以解决问题。问题是我们有很多用户,所以每个人手动这样做是不切实际的。

确切的问题是只有init似乎不起作用。即使在这种混乱的状态下,IE也可以在页面最初加载时向所有其他客户端发布消息,但是在应用了ploicy的IE9上没有返回任何帖子。该应用程序在Firefox和Chrome中运行良好。

我还在web.config中包含了X-UA-Compatible标签但没有用。我是SignalR和KnockoutJS的新手,所以也许我错过了什么?

<script>
    $(function () {

        function Post(message, author, postedDate) {
            this.message = ko.observable(message);
            this.author = ko.observable(author);
            this.postedDate = ko.observable(postedDate);
            this.postedDateText = ko.observable("");
        }

        function PostsViewModel() {
            this.hub = $.connection.messageHub;
            this.posts = ko.observableArray([]);
            this.msg = ko.observable("");
            this.lengthRemaining = ko.observable(1000);

            this.canPost = ko.computed(function () {
                return this.msg().length > 0 && this.msg().length <= 1000;
            }, this);

            if (ko && ko.bindingHandlers) {
                ko.bindingHandlers['jEnable'] = {
                    'update': function (element, valueAccessor) {
                        var value = ko.utils.unwrapObservable(valueAccessor());
                        var $element = $(element);
                        $element.prop("disabled", !value);

                        if ($element.hasClass("ui-button")) {
                            $element.button("option", "disabled", !value);
                        }
                    }
                };
            }

            this.msg.subscribe(function (newValue) {
                this.lengthRemaining(1000 - newValue.length);
            }, this);

            this.updateTime = function () {
                for (var i = 0; i < this.posts().length; i++) {
                    console.log(this.posts()[i].postedDate());
                    var date = new Date(this.posts()[i].postedDate());
                    var curDate = new Date();
                    var diff = curDate - date;

                    if (diff < 60000) {
                        this.posts()[i].postedDateText("Less than a minute ago");
                    } else if (diff >= 60000 && diff < 120000) {
                        this.posts()[i].postedDateText(Math.round(diff / 60000).toString() + " minute ago");
                    } else if (diff >= 120000 && diff < 3600000) {
                        this.posts()[i].postedDateText(Math.round(diff / 60000).toString() + " minutes ago");
                    } else if (diff >= 3600000 && diff < 7200000) {
                        this.posts()[i].postedDateText("one hour ago");
                    } else if (diff >= 7200000 && diff < 86400000) {
                        this.posts()[i].postedDateText(Math.round(diff / 3600000).toString() + " hours ago");
                    } else {
                        var month = date.getMonth() + 1;
                        var day = date.getDate();
                        var year = date.getFullYear();
                        this.posts()[i].postedDateText(year + "/" + month + "/" + day);
                    }
                }                    
            };

            this.sendPost = function () {
                this.hub.server.send(this.msg(), "@User.Identity.Name.Replace(@"ACCOUNTS\","")");
                this.msg("");
                $("#dialog").dialog("close");
            };

            this.cancelPost = function () {
                $("#dialog").dialog("close");
                this.msg("");
            };

            this.init = function () {
                console.log("init");
                this.hub.server.getMessages();
                window.setInterval(function () { postsViewModel.updateTime(); }, 30000);
            };

            this.hub.client.populateMessages = function (postsArray) {
                console.log("populate posts");
                var postsCollection = $.map(postsArray, function (post) {
                    return new Post(post.message, post.author, post.postedDate);
                });

                postsCollection.forEach(function (post) {
                    postsViewModel.posts.push(post);
                });

                postsViewModel.updateTime();
            };

            this.hub.client.updateMessage = function (post) {
                console.log("received a post");
                postsViewModel.posts.unshift(new Post(post.message, post.author, post.postedDate));
                postsViewModel.updateTime();
            };
        };

        var postsViewModel = new PostsViewModel();
        ko.applyBindings(postsViewModel);

        $.connection.hub.start().done(function () {
            console.log("started!");
            postsViewModel.init();
        }).fail(function () {
            console.log("Could not connect!");
        });

        $("#newpost-button").button().click(function () {
            $("#dialog").dialog("open");
        });

        $("#cancel-button").button();
        $("#post-button").button();

        $("#dialog").dialog({
            autoOpen: false,
            height: 256,
            width: 543,
            modal: true,
            close: function () {
                postsViewModel.cancelPost();
            }
        });
    });
</script>

任何帮助将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:1)

@Lukasz如果您将start()代码更改为以下内容会发生什么?

$.connection.hub.start().done(function () {
console.log("started!");
$.connection.messageHub.server.getMessages();

    }).fail(function () {
        console.log("Could not connect!");
    });

答案 1 :(得分:0)

在这种特定情况下,console.log(“”)行在IE7渲染模式下阻塞IE9,强制恢复到IE9模式,并且在它之后的任何行都不会被调用。从代码中删除console.log行修复了问题。