JavaScript Garden. Although JavaScript deals fine with the syntax of two matching curly braces for blocks, it does not support block scope; hence, all that is left in the language is function scope. function test() { // a scope for(var i = 0; i < 10; i++) { // not a scope // count } console.log(i); // 10} There are also no distinct namespaces in JavaScript, which means that everything gets defined in one globally shared namespace. Each time a variable is referenced, JavaScript will traverse upwards through all the scopes until it finds it. In the case that it reaches the global scope and still has not found the requested name, it will raise a ReferenceError.
The Bane of Global Variables // script Afoo = '42'; // script Bvar foo = '42' The above two scripts do not have the same effect. Again, that is not at all the same effect: not using var can have major implications. // global scopevar foo = 42;function test() { // local scope foo = 21;}test();foo; // 21 Local Variables var foo = 3; bar = 4;}test(10); Hoisting. JavaScript DHTML Menu. Cross Browser Drop Down Fly Out Menus. Get Styles. Page last changed today See section 9H of the book for offsetWidth and friends; section 9A for getting styles. The example script doesn't work in Safari Sometimes you'll want to see what styles the default document view has. For instance, you gave a paragraph an width of 50%, but how do you see how many pixels that is in your users' browser? In addition, sometimes you want to read out the styles applied to an element by embedded or linked style sheets.
The style property only reflects the inline styles of an element, so if you want to read out other styles you have to resort to other means. This is our test paragraph with id="test", on which we're going to try our scripts. This is the style sheet of the test paragraph: Before going to the tricky bits, first a nice shortcut that has been inserted into both Mozilla and Explorer: offsetSomething.
The script is quite simple: function getOff() { x = document.getElementById('test'); return x.offsetWidth; } The script is once again fairly simple: Read Parameters Passed Via Query Strings. The only other thing that we need to do is to initialize entries in the array for the fields that we want the page to process and to actually call the azbove function to retrieve the values. If we are adding the code to the top of the purchases page then we are expecting code and desc fields to be passed from the previous page.
Of course the purchases page may be reached without values for these fields being selected so we need to assign default values to the fields before we call the function to load them with the passed values. Here is the code that does this for us. This code also goes into the head section of our page. qsParm['code'] = null; qsParm['desc'] = null; qs(); The values passed from the previous page are now accessible to any Javascript code in the current page by referencing the appropriate qsParm array entries. If (qsParm['code'] && qsParm['desc']) document.write('You selected' + qsParm['code'] + ' : ' + qsParm['desc']);