JavaScript: Namespace error when using xmlDoc.evaluate() - javascript

I'm trying to get an XPathResult object using the evalute() function on a xml document object:
var nodes= xmlDoc.evaluate(XMLPath,xmlDoc,null,XPathResult.ANY_TYPE,null);
even though the namespaceResolver parameter is null and I am using a very simple XML file without any name spaces declared, Firefox throws the following error on the line:
NamespaceError: An attempt was made to create or change an object in a
way which is incorrect with regard to namespaces
Where is my error? Thanks in advance!

Ok, my bad. The error was hiding in the actual XPath I used. Problem solved.

Related

Javascript error when trying to set a variable with PUG variable that doesn't exists

I have the same form for adding and editing an item.
I have passeed a variable from PUG to JAVASCRIPT like this.
script.
var loadedItem = !{JSON.stringify(loadedItem)};
Later i act upon it
if (loadedItem) {
$('select.algs').val(loadedItem.algs).trigger('change');
$('select.ings').val(loadedItem.ings).trigger('change');
$('select.spicy').val(loadedItem.spicy).trigger('change');
$('select.veg').val(loadedItem.veg).trigger('change');
$('select.cats').val(loadedItem.parent_id).trigger('change');
}
As expected when editing everything works fine.
When routing for a new record (not sending and id to load item),
i get a syntax error Uncaught SyntaxError: Unexpected token ';'
The javascript looks like var loadedItem=; which is bad.
Why is this happening? Tnx in advance.
This situation doesn't even let me handle it (can't use if because it just does syntax error for if ())
You can use a ternary operator here to check for a value otherwise output null (or an empty object or some other object from memory):
script.
var loadedItem = !{ loadedItem ? JSON.stringify(loadedItem) : "null" };

What object has a String representation of `{name=value}` in JavaScript?

I receive a complex value in JavaScript (inside the JDK Nashorn engine), that I have to interact with. That value prints to the console as {shown=true}. When I say typeof value I receive object as an answer. When I say Object.keys(value); I receive a TypeError: {shown=true} is not an Object in .... Whey I say value.shown or value["shown"] I always receive a null.
What is type is this mysterious object, and how do I access the value of the "shown" property correctly?
Unfortunately, it is not easy to create a simple example and I cannot debug interactively... Any help is highly appreciated!
Edit:
JDK is JavaSE-1.8.
Calling JSON.parse(value); results in
javax.script.ScriptException: SyntaxError: Invalid JSON: <json>:1:1 Expected , or } but found s
{shown=true}
^
Assuming what you got is a Java object, you should be able to call value.getClass() to get its Java class.
From its string representation, it might be an instance of java.util.HashMap or similar. If so, you should be able to access the value of the "shown" property via value.get("shown").

Get the name of instance as string in Javascript

Im working with Three.js and javascript.
When my code executes this:
console.log(this.scene.children[1])
I get this in my console of Chrome:
How can I get the name of the object ('WidgetsRuler') as a string?
I dont see any attribute that saves this information.
Okay I Solved it using:
console.log(this.scene.children[1].constructor.name)
I believe you should be able to use prototype to achieve this:
Object.prototype.toString
eg:
Console.Log(this.scene.children[1].prototype.toString())
Failing that, you can try constructor:
console.log(this.scene.children[1].constructor.name)

appendChild method not working

Actually, it seems to work in JSFiddle without much problem.
https://jsfiddle.net/3zjqwbgy/5/
However, when I try to run it locally using Notepad++, I get the following error:
Cannot read property 'appendChild' of null
What could be the reason for this? How can I make it work locally?
Make sure you have the HTML available when you run the appendChild method.
That means you'll wrap everything into an load handler:
window.addEventListener("load", function () {
/* your actual code */
...
showElmntProperty("myDeck", "cardName", "first");
});
It's working in JSFiddle because JSFiddle is doing that for you automagically (by default)–you can change it, though:
Since your code works on JSFiddle, the only thing I can think of off the top of my head is you've misplaced your <script></script> tags. Try moving them down below the Table, so that they can be "seen" by JavaScript.
Whenever you get a "Cannot read property "some property name" of null, it simply means that the object that you are calling the method on doesn't actually refer to the object you think it is - it's not pointing to any object at all.
Now, in your code, you didn't say where (exactly) you are getting the error, but take these two lines:
node.appendChild(textnode);
document.getElementById(id).appendChild(node);
If you are getting the error on the first line, then the node variable does not, in fact, refer to anything. If it's the second line you are getting the error on, then document.getElementById(id) isn't returning an actual object, in which case, you'd need to check to see that the id variable you are using there actually matches an id of an HTML element.
And, of course, knowing when your code runs is vital. It may be that:
document.getElementById(id)
Is attempting to locate an element before that element has been parsed into the DOM.

JavaScript json value

I have a JSON var response (See below) but I can't retreive the value of "reputation" using response.users[0].reputation. I am in Dashcode and it says in error Result of expression response.users[unknown] is not an object. What is the correct syntax?
edit: The variable is dynamically loaded from a XMLHttpRequest. A static var with same json is working.
I guess from XMLHttpRequest you recieve string but not json object, so you need parse it first in order to get json object, for example using JSON.Parse or jQuery.parseJSON and response.users[0].reputation should work.
The JSON is fine. I just checked it and "response.users[0].reputation". (Second pair of eyes and all that.)
I would be more concerned about the "unknown" in "response.users[unknown]". It doesn't look like you are requesting the 0th index of the array "users". Something's going wrong there.
Personnally when i test the syntax of your Json, it is correct :
http://www.jsonlint.com
I have just deleted the comment "//need to get its value".
Your code works for me as you have done it. Maybe you are not assigning the variable correctly (var response = {"total": 1, etc...) or have a typo somewhere

Categories