Know if a DOM object is dead [duplicate] - javascript

This question already has answers here:
How can I avoid state of "TypeError: can't access dead object" in my Firefox add-on?
(3 answers)
Closed 7 years ago.
I am working with a variable containing a webpage element such as a button.
However, sometimes I get the error "Can't access to a dead object" because the page containing the element has changed since the moment I saved it.
I would like to know a way to check if an element is dead or not, I tried :
if(element)
alert("Do something");
but it doesn't work as expected.

copied from How to check if element exists in the visible DOM?
var elementInDocument = function(element) {
while (element = element.parentNode) {
if (element == document) {
return true;
}
}
return false;
}
You can use it like:
if(elementInDocument(element))
alert("Do something");

//eval it in your mozilla-browser space
var dc = content.document;
content.document.location.reload();
setTimeout(function(){
try{
dc.parentNode;
}catch(e){
if (e.message.indexOf(' dead ')!=-1){
alert('REALY DEAD!');
}
} }, 1000);
it is a test of dead (in try..catch block) in my moz extension projects.

Related

How can I check if a variable exists (at all) in JS? [duplicate]

This question already has answers here:
JavaScript check if variable exists (is defined/initialized)
(32 answers)
Closed 3 years ago.
Please read the question before marking duplicate. This isn't about an undefined variable. It's about variables which don't exist in the namespace.
I'm working on a codebase which is run in browser and in nativescript. The globals differ between the two. For instance, I'd like to check if window exists at all, but something like this:
if (!!window) {
}
will throw an error:
JS ERROR ReferenceError: Can't find variable: window
Is there a way to test whether or not a variable exists in js (not just undefined)?
You could use a try/catch statement.
try {
if (!!someVariable) {
console.log('yep');
}
} catch {
console.log('nope');
}
You need to use the typeof operator to see if a variable exist or no
you need to test if the variable you are concerned with is undefined of null like so :
if (typeof variable === 'undefined' || variable === null) {
// variable does not exist
}

Javascript - how to check if the parent object is not undefined? [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 5 years ago.
I find I'm doing this, whereby I need to check that the preceeding variable is not undefined before checking the next one in the chain:
if( this.props.someVar && this.props.someVar.data ) {
// do something with this.props.someVar.data ...
}
It would be ideal just to do this:
if( this.props.someVar.data ) {
// This throws an error because I haven't checked if `this.props.someVar` exists beforehand.
}
Is there an easier/shorter way to so this, preferably using pure Javascript?
I hate that too. The easiest way is to use try catch like this
try {
// do something with this.props.someVar.data
} catch (e) {
if (e instanceof TypeError) {
// TypeError happens when an object in the chain is undefined
}
}

Cannot set Border of Undefined - JS [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 6 years ago.
I'm trying to validate some code via javascript. The drama I am having is that I receive an
"Uncaught TypeError: Cannot set property 'border' of undefined".
I am new to javascript and trying to get a full understanding of why this happens and how to prevent this in future coding projects. My goal is if the validation fails it changes the text box border to red.
function validation_Step1(event) {
var Firstbox = document.getElementsByName("Firstbox");
if (Firstbox.value == null || Firstbox.value == '') {
document.getElementsByName("Firstbox")
.style.border = "2px solid red";
alert("Error");
return false;
} else {
return true;
}
}
It's because document.getElementsByName("Firstbox") returns a NodeList which can be seen as a "kind of" array.
You should do document.getElementsByName("Firstbox")[0] if you want to manipulate the first element only
To find elements use ids:
<input type="text" id="login">
Then get the element:
var elem = document.getElementById('login');
Also run JS scripts when whole HTML has been loaded, use onload event in JS.

Check if variable is undefined not working [duplicate]

This question already has answers here:
JavaScript check if variable exists (is defined/initialized)
(32 answers)
Closed 6 years ago.
I want to check if a variable is undefined and quickly found some stackoverflow answers that said the correct way to do it is to test if(variable==null). But in Chrome I am getting an error saying:
Uncaught ReferenceError: xdate is not defined
Huh? The whole reason I am testing is so I don't get errors like this. And I did it just like the approved stackoverflow answers.
Here is my code snippet.
if (xdate == null){
var dateadd = "";
} else {
var dateadd = "&date="+date;
}
use something like this to verify whether variable is undefined or not
if (typeof something === "undefined") {
alert("something is undefined");
}

Prevent stop of code execution on undefined value [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 8 years ago.
Often, I write something like this:
if(model.properties.foo.bar){
// ...
}
else{
// ...
}
Currently, I want to fetch a document only if it isn't already cached. To do so, I write an if-statement like above, checking for the title of the current document.
However, if there is no current document because the user just entered my application, this check will fail due to an undefined model.properties. An error is thrown and the else block won't execute.
What is the commonly best practive to write such checks?
if(model.properties != null){
if (model.properties.foo.bar) {
// ...
} else {
// ...
}
// ...
}
else{
// ...
}

Categories