(1,eval)(code);
new Function('return ' + code)(); // (1,eval)(code);
<audio src="http://developer.mozilla.org/@api/deki/files/2926/=AudioTest_(1).ogg" autoplay> Your browser does not support the <code>audio</code> element. </audio>
preventDefault
for onselectstart
and onmousedown
var element = document.getElementById('content'); element.onselectstart = function () { return false; } element.onmousedown = function () { return false; }
unselectable
attribute $(document.body).attr('unselectable', 'on')
user-select: none
style user-select: none
.g-unselectable { -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; user-select: none; }
Range, TextRange, Selection
<textarea disabled="disabled" style="border:none;color:#000;background:#fff; font-family:Arial;"> JS? </textarea>
$.fn.disableSelection = function() { function preventDefault () { return false; } $(this).attr('unselectable', 'on') .css('-moz-user-select', 'none') .css('-khtml-user-select', 'none') .css('-o-user-select', 'none') .css('-msie-user-select', 'none') .css('-webkit-user-select', 'none') .css('user-select', 'none') .mousedown(preventDefault); .each(function() { this.onselectstart = preventDefault; }); };
(function(){ /* code */ })(); // IIFE (function(){ /* code */ }()); // // "" true && function(){ /* code */ }(); 0,function(){ /* code */ }(); // , !function(){ /* code */ }(); // Facebook style ~function(){ /* code */ }(); -function(){ /* code */ }(); +function(){ /* code */ }(); // new new function(){ /* code */ } new function(){ /* code */ }() // ,
var APPLICATION_ID = function(){ var keys = { 'pew.ru': 15486, 'pew.pew.ru': 15487, 'pew.pew.pew.ru': 1548 }; return keys[window.location.host]; }();
var Module = (function(){ function privateMethod () { }; return { publicMethod: function () { return privateMethod(); } }; }());
We have some DOM object with the parameters “A” initially set with HTML and CSS. In the process, we dynamically change the parameters to "B". Is there an easy way to “reset” the state of an object to the original state “A”? Similar to the events. How to roll back to the original state?Roll back the state of the object does not work. DOM does not store state because elements can be obtained in different ways: directly from html code and generated javascript.
// $('input').click(function(){alert('')}); function resetEvents(element) { var newElement = element.cloneNode(true); element.parentNode.insertBefore(newElement, element); // fixed by @spmbt element.parentNode.removeChild(element); return newElement; } resetEvents($('input')[0]); //
How to correctly transfer js commands with parameters in ajax applications without messing around with quotes? This refers to the data transfer in the ajax system described above. When sending complex commands with several levels of nesting of parameters (for example, "setTimeout ('alert (" Boom !!! ");', 200);") problems are observed when using quotes. How to resolve them or are there any general rules for their design?To send code from the server to the client is bad. This is similar to the problem with eval. Correctly, quickly and safely transmit not the code, but the data that the browser will process.
var nowjs = require("now"); // http var everyone = nowjs.initialize(httpServer); everyone.now.getServerInfo = function(callback){ db.doQuery(callback); }
now.getServerInfo(function(data){ // });
undefined
is sent to reduce the amount of code and work around the problem of overwriting undefined
. jQuery often compares with undefined
and when compressed, undefined
turns into a single-letter variable.window
is passed to reduce the amount of code. jQuery applies a good policy, separating the methods of the window from its own, using window.setInterval()
and others. When compressing, the window
becomes a single-letter variable, the code will be c.setInterval()
DOMMouseScroll
' (Firefox) and ' mousewheel
' (the rest) function wheel(event){ var delta = 0; if (!event) event = window.event; // IE. // delta if (event.wheelDelta) { // IE, Opera, safari, chrome - 120 delta = event.wheelDelta/120; } else if (event.detail) { // Mozilla, 3 delta = -event.detail/3; } if (delta) { // - ( ). if (event.preventDefault) { event.preventDefault(); } event.returnValue = false; // IE // 0, , var dir = delta > 0 ? 'Up' : 'Down', } }
focus
and blur
events: LABEL, INPUT, SELECT, TEXTAREA, and BUTTON. By standard, no other elements can generate this event. For all other elements, there are other DOMFocusIn
and DOMFocusOut
. IE, as always, has its own events: focusin
and focusout
.focus
and blur
.focus, blur, focusin, focusout, DOMFocusIn, DOMFocusOut
:Object.preventExtensions
is the weakest constraint. The object cannot receive additional parameters.Object.seal
- preventExtensions
+ any parameters cannot be deletedObject.freeze
- preventExtensions
+ seal
+ parameters become read onlydocument.createElement('')
and some string methods for creating elements - String.prototype.anchor
and so on. HTMLElement
and HTMLDivElement
are not constructors. // - var HTMLZzzElement = function () { var self = document.createElement('zzz'); self.__proto__ = HTMLZzzElement.prototype; return self; }; // HTMLUnknownElement function F(){} F.prototype = HTMLUnknownElement.prototype; HTMLZzzElement.prototype = new F(); HTMLZzzElement.prototype.constructor = HTMLZzzElement; HTMLZzzElement.prototype.pewpewpew = function () {}; // var zzz = new HTMLZzzElement(); // , zzz.innerHTML = 'Yohoho!'; // console.log(zzz instanceof HTMLZzzElement); // true console.log(zzz instanceof HTMLUnknownElement); // true console.log(zzz instanceof HTMLElement); // true console.log(typeof zzz.pewpewpew); // function // - // DOM document.body.appendChild(zzz); // zzz = document.querySelector('zzz') // console.log(zzz instanceof HTMLZzzElement); // false console.log(zzz instanceof HTMLUnknownElement); // true console.log(zzz instanceof HTMLElement); // true console.log(typeof zzz.pewpewpew); // undefined
HTMLUnknownElement.prototype.pewpewpew = function () {}; console.log(typeof zzz.pewpewpew); // function
Node -> Element -> HTMLElement -> HTMLDivElement/HTMLMetaElement ...
>>document.createElement('div').constructor //undefined -- null...
>>document.createElement('div') instanceof Object //false -- , ? toString >>Object.prototype.toString.call(document.createElement('div')); //"[object Object]"
On the blabla.ru server there is a picture, the server does not give it away if it is transmitted to Referer from another server (pewpew.com). How to get the contents of the picture and show the user?Directly from the browser in any way. You can make a server proxy that will change the referer.
Actually the question is how to repeat the behavior of goto in javascript. (You don’t need to tell about good style and goto :) we are talking about generated code. and the restoration of blocks and cycles process is not quite trivial)JavaScript has tags, but no goto (there is only a reserved word). The label looks the same as in all other languages, but they work differently. The label highlights the block. There may be other marked blocks inside the block. You can jump to the label using break and continue, which are inside this block.
loop1: for (var a = 0; a < 10; a++) { if (a == 4) { break loop1; // 4 } alert('a = ' + a); loop2: for (var b = 0; b < 10; ++b) { if (b == 3) { continue loop2; // 3 } if (b == 6) { continue loop1; // loop1 'finished' } alert('b = ' + b); } alert('finished') } block1: { alert('hello'); // 'hello' break block1; alert('world'); // } goto block1; // Parse error - goto
If there is a desire to use private variables and methods of objects, in JavaScript there is a problem of access to private methods from public ones. And the privileged methods defined inside the constructor, if I understand correctly, are duplicated for each instance, which is not very good in terms of performance. How about this approach?TheShock :
Nice approach. At least better than declaring all the methods in the constructor.
// our constructor function Person(name, age){ this.name = name; this.age = age; }; // prototype assignment Person.prototype = new function(){ // we have a scope for private stuff // created once and not for every instance function toString(){ return this.name + " is " + this.age; }; // create the prototype and return them this.constructor = Person; this._protectedMethod = function (){ // act here }; this.publicMethod = function() { this._protectedMethod(); }; // "magic" toString method this.toString = function(){ // call private toString method return toString.call(this); } };
var Module = (function (window) { function privateFunction() { console.log('pass'); }; var privateClass = function () {}; var publicClass = function () {}; publicClass.prototype.publicMethod = function () { privateFunction(); // ... }; return { // publicClass: publicClass }; }(this)); // (new Module.publicClass()).publicMethod();
When downloading files using File and FormData - how to determine whether the user is trying to load the directory. The File.type property unfortunately does not help (for some types, as well as for the dir - an empty string), the size property does not help either (ff and safari are shown differently)?As for
File.type
, it is said that this is a mime-type and if it is not defined, then it becomes an empty string. mime-type, as usual, is obtained from the file / folder extension. If we drop a real file with the name “pewpew”, then the mime will be "", and if the directory with the name is "pewpew.html", then the mime will be "text/html"
. It does not suit us.SECURITY_ERR
if the user tries to work with incorrect files (protected, not readable, etc.). of this.FileReader
to determine the type of resource. In firefox, if you read a folder as a file, then the reader.readAsDataURL
method throws an exception. In chrome, this exception is not thrown, but the onerror
event is onerror
on the reader
. We cross and get a function to determine whether the file is or not. function isReadableFile(file, callback) { var reader = new FileReader(); reader.onload = function () { callback && callback(true); callback = null; } reader.onerror = function () { callback && callback(false); callback = null; } try { reader.readAsDataURL(file); } catch (e) { callback && callback(false); callback = null; } }
FileReader
behavior may change in the future).Source: https://habr.com/ru/post/124327/
All Articles