Tag: JavaScript

在javascript中将字节数组转换为string

如何将字节数组转换为string? 我发现这些function是相反的: function string2Bin(s) { var b = new Array(); var last = s.length; for (var i = 0; i < last; i++) { var d = s.charCodeAt(i); if (d < 128) b[i] = dec2Bin(d); else { var c = s.charAt(i); alert(c + ' is NOT an ASCII character'); b[i] = -1; } } return […]

如何禁用和重新启用JavaScript中的button?

我可以很容易地禁用一个JavaScriptbutton,它可以正常工作。 我的问题是,当我尝试重新启用该button时,它不会重新启用。 这是我正在做的事情: <script type="text/javascript"> function startCombine(startButton) { startButton.disabled = 'true'; startButton.disabled = 'false'; } </script> <input type='button' id='start' value='Combine Selected Videos' onclick='startCombine(this);'> 为什么这不起作用,我能做些什么来使它工作?

使用jQuery的ajax方法来检索图像作为一个blob

我最近问了另一个(相关的)问题,导致了这个后续问题: 提交数据而不是input表单的文件 通过阅读jQuery.ajax()文档( http://api.jquery.com/jQuery.ajax/ ),似乎接受的数据types的列表不包括图像。 我试图检索使用jQuery.get(或jQuery.ajax,如果我必须)的图像,将此图像存储在Blob中,并将其上传到另一台服务器的POST请求。 目前,它看起来像是由于数据types不匹配,我的图像最终被破坏(字节大小不匹配等)。 执行这个代码如下(这是在咖啡脚本,但不应该很难parsing): handler = (data,status) -> fd = new FormData fd.append("file", new Blob([data], { "type" : "image/png" })) jQuery.ajax { url: target_url, data: fd, processData: false, contentType: "multipart/form-data", type: "POST", complete: (xhr,status) -> console.log xhr.status console.log xhr.statusCode console.log xhr.responseText } jQuery.get(image_source_url, null, handler) 我怎样才能检索到这个图像作为一个blob呢?

什么是ExtJS哲学? 单页面应用程序?

我需要使用ExtJs编写我的下一个项目。 这是一个很好的JavaScript库,但我不完全理解它背后的想法。 以文档页面为例。 我应该用extjs写我的web应用程序吗? 一个永远不会刷新的页面,一切都由AJAX完成? 你如何debugging这样的应用程序,如果得到正确的地方可能需要大量的“点击”和使用它。 您无法修复该错误并在浏览器中进行刷新以查看结果。 有什么build议么?

为什么在使用promise的时候,这个在类方法中是未定义的?

我有一个JavaScript类,每个方法返回一个Q诺。 我想知道为什么this是未定义在method2和method3 。 有没有更正确的方法来编写这段代码? function MyClass(opts){ this.options = opts; return this.method1() .then(this.method2) .then(this.method3); } MyClass.prototype.method1 = function(){ // …q stuff… console.log(this.options); // logs "opts" object return deferred.promise; }; MyClass.prototype.method2 = function(method1resolve){ // …q stuff… console.log(this); // logs undefined return deferred.promise; }; MyClass.prototype.method3 = function(method2resolve){ // …q stuff… console.log(this); // logs undefined return deferred.promise; }; 我可以通过使用bind来解决这个问题: […]

D3 4.0的rangeRoundBands相当于?

我看到很多D3代码都是这样的: var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1); 从D3版本4.0开始, d3.scale.ordinal()现在是d3.scaleOrdinal , rangeRoundBands似乎不见了。 > d3.scaleOrdinal() { [Function: scale] domain: [Function], range: [Function], unknown: [Function], copy: [Function] } 这个代码(来自Mike Bostock的条形图示例 )的D3 v4等价物是什么? var x = d3.scale.ordinal() .rangeRoundBands([0, width], .1);

Javascript – 对象键 – >值

var obj = { a: "A", b: "B", c: "C" } console.log(obj.a); // return string : A 但我想通过这样的variables来获得 var name = "a"; console.log(obj.name) // but return undefined 这个怎么做?

在JavaScript中打破NaN

是否有任何现代浏览器引发NaN传播的exception(即将数字乘以或添加到NaN),还是可以configuration为这样做呢? 沉默的NaN传播是一个可怕的阴险的错误来源,我希望能够尽早发现它们,即使在性能上也是如此。 这里有一个use strict的例子, jshint等。 不会拿起: object = new MyObject(); object.position.x = 0; object.position.y = 10; // … lots of code var newPosition = object.position + 1; // <- this is an error, and should // have been object.position.x // however it fails *silently*, // rather than loudly newPosition *= 2; // <- this doesn't […]

在asp.net中的DateTime JavaScript序列化不给一个JavaScriptdate对象?

当我在.Net中parsing一个DateTime到json时,它返回一个string(即"\/Date(1249335194272)\/" )。 我如何使它返回一个js Date对象构造函数不包裹在一个string? // js server code var dteNow = <%= jsonDateNow %>; // js rendered code var dteNow = "\/Date(1249335477787)\/"; // C# using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.Script.Serialization; using System.Web.UI.WebControls; namespace testing{ public partial class iTaxPrep : System.Web.UI.Page { protected string jsonDateNow; protected void Page_Load(object sender, EventArgs […]

Express.js查看“全局”

我正在使用Express.js(在Node.js上),我知道你可以通过“locals”参数来显示自定义数据的视图。 ( res.render("template", { locals: { foo: "bar" } }); ) 有什么办法可以有“全局”? (即每个视图都可以访问的数据) 我看到view options ,但这不是recursion的,所以如果我用我的模板使用任何本地人,它取代了我设置的本地人。 这是我的用例:我想这样做,以便每个页面可以添加CSS / JS文件,这是我的主要布局的一部分。 问题是,如果我没有明确地在每个渲染上设置这些数组,我得到一个未定义的错误,所以在我的模板中,我总是要做typeof css !== "undefined" dance。 另外,我还有其他的select框选项列表,我不想明确地添加到每个表单中。