This is not entirely new information, however, it was new to myself and many others. I recently discovered that in JavaScript, one can call methods on items from the DOM without first declaring them as a variable with the standard
'var varName = document.getElementById("someElement");'.
For example assuming you had a paragraph tag with the id of 'someElement', the following JavaScript code:
someElement.innerHTML="I am some new text";
Accomplishes the same result as:
var someElement = document.getElementById("someElement"); someElement.innerHTML="I am some new text";
However, as with many things, just because it works doesn't mean it should be done. The small amount of information I was able to acquire about this strange occurrence stated that it is bad practice, and should be avoided, and relying on the browser to create your variables is never a good idea. I have verified that this works on Internet Explorer and Chrome, but have not checked Firefox/Safari/Opera etc.
So don't try this at home, folks. (Or do; just not in a live/professional setting)
I tested it in the other three browsers (Firefox, Safari, and Opera) and it successfully rendered the text. I do agree that this is not a good shorthand to adopt as everyday usage.
ReplyDeleteOh, great to hear that it works on all of the major browsers. Doubt I will ever use it; though it could be useful for rapid mockups and the like. Still, not reliable enough to ever, ever, ever consider on a live environment.
ReplyDelete