Fastest way to duplicate an array in JavaScript – slice vs. ‘for’ loop

There are at least 6 (!) ways to clone an array: loop slice Array.from() concat spread operator (FASTEST) map A.map(function(e){return e;}); There has been a huuuge BENCHMARKS thread, providing following information: for blink browsers slice() is the fastest method, concat() is a bit slower, and while loop is 2.4x slower. for other browsers while loop is the fastest method, since those browsers don’t have internal optimizations for slice and concat. This remains … Read more

jQuery Ajax File Upload

File upload is not possible through AJAX.You can upload file, without refreshing page by using IFrame.You can check further details here. UPDATE With XHR2, File upload through AJAX is supported. E.g. through FormData object, but unfortunately it is not supported by all/old browsers. FormData support starts from following desktop browsers versions. IE 10+ Firefox 4.0+ Chrome 7+ Safari 5+ Opera 12+ For … Read more

How do I embed PHP code in JavaScript?

If your whole JavaScript code gets processed by PHP, then you can do it just like that. If you have individual .js files, and you don’t want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript. For example, in your index.php (or wherever you specify your layout), you’d do something … Read more

Difference between this and self in JavaScript

Unless set elsewhere, the value of self is window because JavaScript lets you access any property x of window as simply x, instead of window.x. Therefore, self is really window.self, which is different to this. If you’re using a function that is executed in the global scope and is not in strict mode, this defaults to window, and therefore If you’re using a function in a different context, this will refer to that context, but self will still … Read more

jQuery AJAX submit form

You can use the ajaxForm/ajaxSubmit functions from Ajax Form Plugin or the jQuery serialize function. AjaxForm: or ajaxForm will send when the submit button is pressed. ajaxSubmit sends immediately. Serialize: AJAX serialization documentation is here.