Fix the typo I said in comment and also @Bonakid mentioned.
globOject.counterClick(0), true);
should beglobObject.counterClick(0), true);
Do not use
globObject.counterClick(0)
for callback function, useglobObject.counterClick
instead. The first one will get called immediately whenglobObject
is defined.Do not use
this.sendToServer();
, useglobObject.sendToServer();
instead.this
in thecounterClick
method will bedocument.getElementById('submbut')
which is a HTML element. Addconsole.log(this)
tocounterClick
and you will see that.
Object causing an undefined error in console
-
01-07-2022 - |
Question
There wasn't something close enough to this already... at least that my newbie mind could grasp.
I'm just trying to play around creating a global containing object that has methods, and contained within are the instructions to create and use AJAX requests. I then proceed to create an event listener for a button. I then get the following console output.
Port: Could not establish connection. Receiving end does not exist. -This was deleted-.net/:1
Uncaught ReferenceError: globOject is not defined script.js:21
Port: Could not establish connection. Receiving end does not exist. -This was deleted-.net/:1
Exception in onResRdy: TypeError: Cannot read property 'htmlRes' of undefined ContentScript.js:84
I'm sure I'm doing a lot wrong here. Let me know what additional info I would need to include to get assistance on this.
var globObject = {
sendToServer: function () {
alert('you got to me at least!');
var xhr;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xhr=new XMLHttpRequest();
}
else {// code for IE6, IE5
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open('POST', 'counterDoc.php', true);
xhr.send();
},
counterClick: function (counter) {
alert('you got here!');
counter += 1;
this.sendToServer();
}};
var button = document.getElementById('submbut').addEventListener('click',globOject.counterClick(0), true);
Solution
OTHER TIPS
change this
var button = document.getElementById('submbut').addEventListener('click',globOject.counterClick(0)
to
var button = document.getElementById('submbut').addEventListener('click',globObject.counterClick(0)
theres a typographical error in this part
globOject.counterClick(0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow