小编Fat*_*cet的帖子

未捕获的InvalidStateError:无法在'WebSocket'上执行'send':仍处于CONNECTING状态

当我的页面加载时,我尝试向send服务器发送一条消息以启动连接,但它无法正常工作.此脚本块位于我的文件顶部附近:

var connection = new WrapperWS();
connection.ident();
// var autoIdent = window.addEventListener('load', connection.ident(), false);
Run Code Online (Sandbox Code Playgroud)

大多数时候,我看到标题中的错误:

未捕获的InvalidStateError:无法在'WebSocket'上执行'send':仍处于CONNECTING状态

所以我尝试catch了异常,正如你在下面看到的那样,但现在它似乎InvalidStateError没有定义,并产生了一个ReferenceError.

这是我的websocket连接的包装器对象:

// Define WrapperWS

function WrapperWS() {
    if ("WebSocket" in window) {
        var ws = new WebSocket("ws://server:8000/");
        var self = this;

        ws.onopen = function () {
            console.log("Opening a connection...");
            window.identified = false;
        };
        ws.onclose = function (evt) {
            console.log("I'm sorry. Bye!");
        };
        ws.onmessage = function (evt) {
            // handle messages here
        };
        ws.onerror = function …
Run Code Online (Sandbox Code Playgroud)

javascript exception-handling websocket

35
推荐指数
3
解决办法
4万
查看次数

在"原型"中设置"构造函数"属性的优点

在JavaScript Prototype继承中,添加prototype.constructor属性的目标是什么.让我举个例子来解释一下.

var Super = function() {
    this.superProperty = 'Super Property'
}
var Sub = function() {
    this.subProperty = 'Sub Property'
}

Sub.prototype = new Super();
Sub.prototype.constructor = Sub; // advantages of the statement

var inst = new Sub();

在添加Sub.prototype.constructor = Sub时,以下行在所有情况下都返回true.

console.log(inst instanceof Sub)   // true
console.log(inst instanceof Super) // true

我猜,它在获取新实例时可能会有用但是何时和/或如何?

提前致谢.

javascript inheritance constructor prototype

16
推荐指数
1
解决办法
2413
查看次数

在IE上获取图像加载时的图像宽度失败

我有一个图像缩放功能,可以按比例调整图像大小.在每个图像上加载一个调用此函数的图像,如果其宽度或高度大于我的最大宽度和最大高度,则调整大小.我可以在FF Chrome Opera Safari中获得img.width和img.height但是IE失败了.我怎么处理这个?

让我用一段代码解释一下.

<img src="images/img01.png" onload="window.onImageLoad(this, 120, 120)" />

function onImageLoad(img, maxWidth, maxHeight) {
     var width = img.width; // Problem is in here
     var height = img.height // Problem is in here
}

在我的高亮度行中,img.width不适用于IE系列.

有什么建议吗?

谢谢.

javascript image image-loading

11
推荐指数
1
解决办法
1万
查看次数

使用Ext JS进行jQuery深度复制?

我已经尝试过并且惊讶于如何使用ExtJS.让我用代码块解释一下.

在jQuery中

console.clear();
var a = {
    b: 5,
    c: 4,
    o: {
        l: 2,
        p: 2
    }
}

var b = {
    k: 4,
    l: 3,
    c: 5,
    o: {
        m: 2,
        l: 1
    }
}

var ex = $.extend(true, a, b);
console.dir(ex)

这是输出

ex = {
    a: {
        q: 2
    },
    b: 5,
    c: 5,
    o: {
        l: 1,
        p: 2,
        m: 2
    }
}

Ext apply,applyIf,copyTo不能像这样工作.如何在ExtJS中生成输出?

提前致谢.

extjs object deep-copy extend

8
推荐指数
2
解决办法
4573
查看次数

如何知道拖动元素是哪个元素?

我有jQuery Drag&Drop的问题.我有一个draggable元素和很多dropTarget所以我只想知道我什么时候拖动,我的拖动元素是哪个元素?

顺便说一句,Firefox提供了指向DOM元素的event.originalTarget,但在Google Chrome中它等于"undefined".

有什么建议吗?

谢谢.

jquery drag-and-drop jquery-ui draggable jquery-ui-draggable

5
推荐指数
1
解决办法
3353
查看次数

通过"this"与"prototype"分配功能有什么区别?

可能重复:
在Javascript中使用'prototype'与'this'?

我对这两种向函数添加方法的方式感到困惑.让我举个例子来解释一下.

var foo = function(){
    this.bar = function(){alert('I am a method')}
}

foo.prototype.baz = function(){alert('I am another method')}

var car = new foo();

现在,在这一点上我们可以使用汽车的baz和bar方法.好吧,但它们之间有什么区别.为函数原型或它的构造函数添加方法的细微差别是什么.

谢谢..

javascript oop constructor prototype function

5
推荐指数
1
解决办法
771
查看次数

在ExtJS中禁用属性网格上的自动排序

我正在处理财产网格.我想阻止属性网格的列名称的自动排序.这是我的代码.大胆突出显示的代码是我的属性网格源,它的顺序就像我想看到的那样.但Ext是按字母顺序自动排序列顺序.我怎么能防止这种情况.

谢谢你的任何建议.

Ext.ns('Application.propertygrid');
Application.propertygrid.FileDetail = Ext.extend(Ext.grid.PropertyGrid, {
    title: 'File Detail',
    height: 200,
    border: false,
    stripeRows: true,
    flex: 1,
    initComponent: function () {
        Application.propertygrid.FileDetail.superclass.initComponent.apply(this, arguments);
    },
    source: {
        Name: 'Please select a file',
        Type: 'Please select a file',
        Size: 'Please select a file',
        Path: 'Please select a file',
        FullPath: 'Please select a file',
        Width: 'Please select a file',
        Height: 'Please select a file'
    },
    listeners: {
        beforeedit: function(){
            return false; // prevent editing 
        },
        headerclick: function(){
            return false; // prevent column …

propertygrid extjs

5
推荐指数
2
解决办法
1万
查看次数