Are Assumed Variables Safe to Use? [duplicate] - javascript

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Closed 7 years ago.
I've always noticed that Chrome assumes variables based upon elements within an HTML document that have an ID.
For example, if I had <video id="video" src="foo.webm"></video> on my page, the variable "video" would be created automatically by the browser. No need to type out var video = document.getElementById("video");
Is this a standard browser feature? Is it something that is here with us to stay? Should I take advantage of this or be wary of it? I like it because it saves precious bytes, especially when you have a ton of element objects to define.

Variables are not created that way.
document.getElementById("video") means you are selecting the "video" element, not variable. Checkout CSS Selectors

Related

What is the difference between parentNode.removeChild() and remove() [duplicate]

This question already has answers here:
What is the difference between 'remove' and 'removeChild' method in JavaScript?
(2 answers)
Closed 2 years ago.
I am developing a website. There are a lot of spots, where I have to remove the html element. The previous, main contributor used to do it like this:
var elem = document.getElementById('element');
elem.parentNode.removeChild(elem);
I'm wondering why didn't he just use
elem.remove();
instead.
I've gotten to the point, where the first method didn't work and returned an error, but the second one worked perfectly. Of course I wanted to stick with the code standard in this project, so my first try was to use parentNode.removeChild. Unfortunatelly I cannot contact that person to ask why is it done like that.
What is the difference between these two and can I safely replace those?
As from MDN, the two are equivalent. The .remove() was inspired by jQuery and was implemented much later. IE does not support it.
If you don't need IE, you can safely replace parentNode.removeChild, but if you will transpile the code, the replace method have polyfill using parentNode.removeChild method...
Source of the answer
How is remove different from removeChild?
remove only needs a reference to the child. removeChild needs a reference both to the parent and the child. The result is identical.
Also you might think ,Is there any way to ensure that element is actually removed from memory?
No. You can only unreference it and hope that there is a garbage collector which will detect the object is not referenced and then will remove it.

js get element from window['id'] vs document.getElementById('id') [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Is there a spec that the id of elements should be made global variable?
(5 answers)
Closed 4 years ago.
What is better and what is the difference (performance, browser support...)?
I thought that DOM elements that have an id automatically create in a window object so you don't need to find them in DOM through getElementById. Am I right and what problems it could bring? I can't find any documentation on such a possibility of js window object.
Examples with window:
window['elementId'];
window.elementId;
The same result as getElementById and querySelector

Difference between javascript:fnName() and normalfnname() [duplicate]

This question already has answers here:
Do you ever need to specify 'javascript:' in an onclick?
(8 answers)
Closed 8 years ago.
Can someone please explain the difference between:
onclick="javascript:fnName(this);"
...and...
onclick="fnName(this);"
Is there any performance hit? or when to use what?
Not actually, and usually you don't write the first one. Because onclick handler is handled by the JavaScript, so calling JavaScript to handle it won't be a good bet.
In the context of event attributes it's completely useless. It's mainly used inside of the href attribute and allows you to create a pseudo-onclick event:
click me
Also you can just put it in the location input in your browser and run scripts. It's simillar to the console input.
Edit: I realized it's not really useless but it has completely different usage than you would thought. In the context of these event attributes it behaves like in a normal code and so it's a label. Try this:
<div onclick="javascript:while(true){while(true){alert('hello');break javascript;}}">click me</div>

Selecting elements without selectors, using element ID only [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Closed 8 years ago.
I've always had to use a selector to get elements in the DOM so I could retrieve the data that I need. eg:
var test1 = document.getElementById('testElement').value; // or
var test2 = document.querySelector('#testElement').value; // or
var test3 = $('#testElement').val(); // etc...
Recently, I noticed that I no longer need to do that. Instead, simply using the element's id seems to be sufficient. It seems to be used as a reference to the element. The code below works for me in Chrome, Firefox and even IE9.
var test4 = testElement.value;
I've been trying to find some more information on this but wherever I look, everyone says that selectors need to be used.
So either my colleagues and I completely missed this somehow, or not many people know about this. Or, I suppose, I'm horrible at searching for information.
Basically, I'm looking for more information on this.
So please point me in the right direction so I can investigate further and make sure this functionality is here to stay and can be used consistently.
This is a browser feature, which is not according to the specification. This means that for your code to work on most browsers, it should be using some selector. Also, testElement may not work if it already is a global variable.
Depending on the browser the element id may be in the global scope. That is a nice feature and all but I would not depend on this for your applications. If you want to be thorough I suggest using getElementById() or a selector with jQuery.

Javascript: add attribute method question [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript setAttribute vs .attribute=
Should I prefer one of these two methods to add attributes?
input.name='amount';
input.setAttribute('name', 'amount');
setAttribute doesn't work correctly in IE
http://www.quirksmode.org/dom/w3c_core.html#attributes
From MDN:
Using setAttribute() to modify certain attributes, most notably value
in XUL, works inconsistently, as the attribute specifies the default
value. To access or modify the current values, you should use the
properties. For example, use elt.value instead of
elt.setAttribute('value', val).
I guess it would be better to access attributes directly, since that is what the DOM is designed for...
Mozilla supports only setAttribute(). So if you are designing a multi-browser supporting page, it will be good to have an if condition checking the Browser Name, and call the function/assignments as per that.

Categories

Resources