Firebug is complaining about this line:
$("#original-description").text(response['course']['original_description']).hide();
Do I have a syntax error? Looks fine to me.
More context:
bindOnSuccess($('#course-search'), function(response) {
if (!response) {
$("#system-status").text("Sorry, no course could be found for that search.");
}
else {
$(".dept-code").text(response['course']['dept_code']);
$(".course-number").text(response['course']['number']);
$(".course-title").text(response['course']['title']);
$("#div-original-description").show();
$("#original-description-teaser").show();
// error here
$("#original-description").text(response['course']['original_description']).hide();
$("#td-required-for").text(response['analysis']['cRequiredFor']);
}
});
response is a JSON object. Could this problem be caused by invalid subscripts?
Firebug's error is:
$("#original-description").text(response.course.original_description).hide is not a function
The other answers are pointing out incorrectly - .text() returns the jQuery object. You are probably referencing an undefined property. I can replicate this:
$('<p>').text(undefined).hide()
Make sure you are referencing the right property in the JSON.
TypeError: $("<p>").text(undefined).hide is not a function { message="$("<p>").text(undefined).hide is not a function", more...}
If you want to query the object live you can simply do
window.o = response in your callback function and just play around with it in Firebug console.
Related
I can reference this data fine:
$("#treelist").data('kendoTreeList').dataSource.options.batch (console shows false)
But I can't seem to reference this data:
$("#treelist").data('kendoTreeList').dataSource._pristineData[0].DepartmentCode
It says: Uncaught TypeError: Cannot read property 'DepartmentCode' of undefined
So I tried this:
$("#treelist").data('kendoTreeList').dataSource._pristineData
and it just shows this:
How can I get 080 DepartmentCode?
I tried this $("#treelist").data('kendoTreeList').dataSource._online and got true
Thanks!
Looks like I was able to get the value by invoking DataBound:
events.DataBound("dataBound");
function dataBound(e) {
console.log(e.sender.tbody[0].firstElementChild.cells[1].innerText);
}
It's not pretty but it works.
I'm guessing the answer to my question is no, but since I don't know enough about how Error.prototype works I figured it was worth asking: is there any way to change the error messages from the errors in Javascript?
For instance, if I get an error:
TypeError: (intermediate value) is not iterable
is there any way to change things such that I instead get:
TypeError: You expected an array but it wasn't an array dummy!
I thought about using a global error handler and then rethrowing them, but that would only work for uncaught errors. Is there any Error.prototype method I can change (or any other way) to do this?
Not at all important, just curious.
EDIT: Just to clarify two points:
1) I understand how try/catch works, but what I'm asking about is whether there is a way to control the messages generated before the error is thrown/caught (presumably by overwriting a method of Error.prototype).
2) An answer of "no there is no way to do this, all generating of JS error messages is handled internally and the JS code has no way to control it" would be perfectly legitimate (... if that's the case).
You have to override the TypeError class, not one of these methods.
const overridableMessages = [{
search: 'TypeError: (intermediate value) is not iterable',
replace: 'TypeError: You expected an array but it wasn\'t an array dummy!'
}]
class TypeError extends window.TypeError {
constructor (message) {
super(message)
overridableMessages.forEach((overridableMessage) => {
if (this.message === overridableMessage.search) {
this.message = overridableMessage.replace
}
})
}
}
window.TypeError = TypeError
window.onerror = function(msg, url, linenumber) {alert('Error : '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);return true;}
// general error handler
Also:
try {
}
catch(err) {
alert(err); // here put your case with your messages.
}
// routine error handler
HIH,
I hate to answer my own question, but I found this line on the MDN which seems to pretty clearly answer things:
The global Error object contains no methods of its own, however, it does inherit some methods through the prototype chain.
Since the only methods it gets through the prototype chain are toString and toSource (neither of which controls an error's generated message), it appears (unless I'm missing some other mechanism that isn't Error.prototype-related) that there is no way to accomplish what I asked.
I want to iterate over all the forms present in a div. So I am using the following code for this
$('#divid form').each(function (index, formDetails) {
if (formDetails) {
console.log($(formDetails).attr('id'));
}
});
This is working fine in Mozilla with no issues but when I run this code in Chrome sometimes it throws the following error.
This error is coming
Uncaught TypeError: Cannot read property 'Constructor' of undefined
I am using Version 33.0.1750.117 m of Chrome.
Why this error is coming I am not able to understand?
Sounds like you don't have jQuery included before your try and load your functions.
Wrap your javascript code inside the below function:
$(document).ready(function() {
alert('loaded');
}
Also check if the initial is $ or jQuery
function Notify(header,content,image){
var note = webkitNotifications.createNotification(image||"",header,content);
note.show();
return note;}
var extensions = ["pbjhaapnigfhipfahcfkeakpcgkmnklc"];
function CheckReload(){
for(var CN=0;CN<extensions.length;CN++){
var id = extensions[CN];
var ex = chrome.management.get(id);
console.log("Checking",ex,"-",id);
if(!ex.enabled){
Notify("Extension reloaded!",ex.name+" was found crashed, and reloaded!");
chrome.management.setEnabled(id,true);
}
}
}
setInterval(CheckReload,1000);
Ok, so what I was expecting was for this to check the extensions in the "extensions" array, and if they weren't enabled it would create a notification saying that it wasn't, and then enable it.
However, chrome.management.get(id) seems to be returning undefined.
I expected an output like:
Checking Object - [id]
Instead, what I got was:
Checking undefined - pbjhaapnigfhipfahcfkeakpcgkmnklc
Uncaught TypeError: Cannot read property 'enabled' of undefined
How can I fix this?
Most of the methods provided by chrome don't return a value, instead they take a callback function as a parameter, and call that function with the wanted result.
You should replace your code by
chrome.management.get(id, function(ex) {
console.log("Checking",ex,"-",id);
if(!ex.enabled){
Notify("Extension reloaded!",ex.name+" was found crashed, and reloaded!");
chrome.management.setEnabled(id,true);
}
});
See http://developer.chrome.com/extensions/management.html#method-get for details.
If you are running your code from an extensions, make sure that your extension have permissions to management.
So I am deleting all the contents under a particular div and adding a message content. However, javascript throw the following error after the finish:
Uncaught Error: NOT_FOUND_ERR: DOM Exception 8
Here is the code where it is executed
new Ajax.Request("profileThis.php",
{
method:'post',
parameters:{title:title, review:review, userId:userId, category:category, categoryId:categoryId},
onSuccess:function(ajax)
{
alert(ajax.responseText); // this is just for debugging purposes
var message=ajax.responseText;
var divMessage=document.createElement("div");
divMessage.style.color="rgb:(105,105,105)";
divMessage.innerHTML=message;
while($("reviewSheet").hasChildNodes)
{
$("reviewSheet").removeChild($("reviewSheet").lastChild);
}
$("reviewSheet").adopt(divMessage);
},
onFailure:ajaxFailure,
onException:ajaxFailure
});
People commented that the problem was with how I assigned divMessage to reviewSheet. I tried both adopt and appendChild but none works.
A little help would be appreciated.
divMessage.style.color="rgb:(105,105,105)";
should be
divMessage.style.color="rgb(105,105,105)";
Is the problem that you are calling the method hasChildNodes() on a jQuery object? I'm not sure what $("reviewSheet") is supposed to be, but wrapping a string in $() makes it a jQuery object which I don't believe will work with regular javascript methods. If "reviewSheet" is the id of an element you could do something like
node = document.getElementById('reviewSheet');
then you could go into your while loop.
while (node.hasChildNodes()) {
//the rest of your code here
}
Oh also you need to put the parenthesis after hasChildNodes() to return a boolean value.