<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d8211560\x26blogName\x3dTech+Tips,+Tricks+%26+Trivia\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://mvark.blogspot.com/search\x26blogLocale\x3den\x26v\x3d2\x26homepageUrl\x3dhttp://mvark.blogspot.com/\x26vt\x3d-5147029996388199615', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

Tech Tips, Tricks & Trivia

by 'Anil' Radhakrishna
A seasoned developer's little discoveries and annotated bookmarks.

Search from over a hundred HOW TO articles, Tips and Tricks


JavaScript Questions & Answers - 2

These questions & answers are based on the material from the book Eloquent JavaScript by Marijn Haverbeke, which covers ECMAScript 5 and from the most voted up questions on Stack Overflow.

Q. Why do Google & some other popular websites prepend while(1); to their JSON responses?
A. It prevents JSON hijacking.  Long answer
Facebook prepends for (;;); before JSON content. for (;;); is a way to intentionally create a loop that doesn't terminate on its own.

Q. What does “use strict” do in JavaScript?
A. ECMAScript 5 Strict mode helps out in a couple ways:
~ It catches some common coding bloopers, throwing exceptions.
~ It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
~ It disables features that are confusing or poorly thought out.

Normally, when you forget to put var in front of your variable, as with counter in the example, JavaScript quietly creates a global variable and uses that. In strict mode, however, an error is reported instead.

If you put "use strict"; in your code, and run it in a browser that doesn't support it, the statement has no effect. If you run it in a later browser, the browser will check for more errors.

You can apply "strict mode" to the whole file or you can use it only for a specific function

It's supported by all major browsers.

Q. When building a link that has the sole purpose to run JavaScript code, which is better - using “#” or “javascript:void(0)”?
A. Neither. Long answer

Q. Comments in JSON are not allowed. True or False
A. True

Q. What problem does JSONP solve?
A. JSONP or JSON with Padding, is really a simply trick to overcome XMLHttpRequest same domain policy.

Q. What does the debugger statement do?
A. A debugger statement (consisting of simply that keyword) can be used to set a breakpoint in your program. If the Developer tools of your browser are active, the program will pause whenever it reaches that statement, and you will be able to inspect its state.

Q. JavaScript doesn’t provide direct support for selectively catching exceptions: either you catch them all or you don’t catch any. True or False
A. True

Q. What does this statement do:
alert($("*:focus").attr("id"));
A: Alerts id of currently focused element

Q. Other than by using eval, how can you interpret data as code?
A: A better way of interpreting data as code is to use the Function constructor. This takes two arguments: a string containing a comma-separated list of argument names and a string containing the function’s body.
var plusOne = new Function ("n" , "return n + 1;") ;
console.log(plusOne(4)) ;
//  5

Q. What is the difference between the keypress and keydown events?
A:  "keypress", which is fired right after "keydown" (and repeated along with "keydown" when the key is held) but only for keys that produce character input.

Labels:

Tweet this | Google+ it | Share on FB

« Home | Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »
| Next »

»

Post a Comment