Why can't you stringify a jQuery object? - javascript

The line JSON.stringify( $("p") ); causes an error:
InvalidStateError: Failed to read the 'selectionDirection' property from 'HTMLInputElement': The input element's type ('button') does not support selection.
(I'm using Google Chrome 34)
Why?
How else should I make $("p") more portable so I can store it or pass it in a message?

There's a ton of state (attributes, event handlers, the code related to those, internal state, ...) involved in an HTML element. It just doesn't make sense to serialize all of that into JSON.
If you want to get some kind of representation of the element in JSON, you could for instance use .html() to get a HTML string representing the element. Or come up with a format that encodes, for instance, tag names, attributes and text only. You could have to implement that by hand though (or find a library - "html to json" could be a good keyword)

Related

how to access the element by the value stored in variable in javascript

Basically i have an html collection coming up with:
document.getElementsByName('the name')
It consists of a select which has options and i want to get the length of the value which works in internet explorer as well as edge browser so i thought of getting it by:
document.getElementsByName('the name').thename.options.length
I am getting the value in the console part of my browser but while writing the code and running it is not able to access the thename due to some rules I don't know
so is there any way i can get the access of the .thename. inside the document.getElementsByName('the name').thename.options.length
I hope I am clear with my question as this is an ongoing project and I can hardly make other changes
thename is not a built-in object to the HTML select element, so you must look for the correct syntax.
I'm assuming you're looking for something like this:
To find the nth option, you would do: document.getElementsByName('the name')[n-1](as this is zero-indexed).
Then you can proceed with the .value and .length methods.
document.getElementsByName('the name')[n-1].value.length
In addition, since this is a syntax error, you may look to online resources first before consulting StackOverflow, as this is general knowledge. Trying to make up syntax or thinking something may be right is also a no-no. Computers are stubborn, and we must be too with them.
More info: https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement

Blue Prism: Cannot Iterate through JSON items withing injected JS script

So I am trying to iterate through JSON items via injected script. And BP is throwing me a syntax error that it is incapable of compiling my code.
code:
function fillInputs(json){
alert(json[0].Email);
}
through Invoke I am giving [JSON] variable that has a well structured JSON object which I created with help of Utility - JSON and it is working fine on test websites. But BP will not initialize specifically this part / alert(json[0].Email); / and is throwing a standard error which usually means syntax errors.
Could someone tell me if there is a better way to iterate through JSON objects with Blue Prism injected Javascript code and if I am choosing a harsh way to do it?
This is easily accomplished using the built-in Internet Explorer browser automation features provided out of the box in Blue Prism.
For organization, place your custom Javascript in a data item of type text on your action page:
Then use a Navigate stage to insert the fragment and invoke it:
With your current snippet, you'll get a nice alert window in Internet Explorer:
A good way to retrieve this data using Blue Prism would be to use the Javascript snippet you insert to create a hidden input element, the value of which is set a string representation of the JSON data you're attempting to exfiltrate.
You can then create a manual Application Modeler entry based off of an existing one to target this hidden element, and then use a Read stage to copy the data into a Data Item within Blue Prism. Further, you can use the bundled JSON object (or write your own as a wrapper around a better VB/C# library) to extract and manipulate the data within as you wish.

Python-Selenium-Chrome: how to get access to the whole document property

I need to get the whole html code of a page which uses JavaScript. I use Selenium module with Chrome and see different 'find' methods, but didn't find some sort of ".document" property (like ie.document in VBA). Is there some possibility to get the whole result code (after JS execution) in string format for example?

Using jQuery data() vs native javascript getAttribute vs input hidden

I need to access general information from my site using javascript. So far, I have the following options:
Using an html element :<input type="hidden" value="MyValue"/>
Using a custom attribute in an existing html element : <div id="HtmlElement" myattr="MyValue"></div> and then access it using document.getElementById("HtmlElement").getAttribute("myattr")
Using a data attribute in an existing html element: <div id="HtmlElement" data-myattr="MyValue"></div> and then access it using jQuery("#HtmlElement").data("myattr")
I was wondering what are the benefits of using either option.
I'm not a fan of using hidden inputs because I don't like the idea of having a loose html element that contains information. But since I need it to display general information, not information related to an existing html element in the page, it doesn't seem so bad.
On the other side, I'm not a fan of abusing the use of an external library but in my case I'm allready loading jQuery in my site, so it's not as if i was loading an entire library just for this.
And finally, even dough performance is allways an issue, in my case it's not gonna make much difference if it's the fastest solution.
I would go with data attributes because that's what they are for and modern browsers have a native api for accessing them, while still allowing non-modern browsers to access them as custom attributes.
given this html:
<div data-foo="bar"></div>
// modern browser:
foo = document.getElementByID("myelementid").dataset.foo;
// older browser:
foo = document.getElementByID("myelementid").getAttribute("data-foo");
// jQuery (all browsers)
foo = $("#myelementid").data("foo");
Note if your data attribute is data-foo-bar, the key in dataset and .data will be fooBar
As sdespont mentions, the data should be relevant to the element you are storing it on.
Update:
It's also important to note that you can also get the value of a data attribute using the .attr method, however there is a pretty important difference between the two. Getting a data attribute's value with .data will attempt to parse the value of the attribute into the correct javascript type, for example, if it can be converted to an int, it will be converted to an int. If it can be converted into an object or an array, it will be converted to an object or an array. If you instead used .attr(), you can be sure that you will always have a string.
Probably also worth mentioning that using .attr() should be prefered over .data() unless you specifically need the features given by .data() due to the fact that by using .data(), a data cache will be created for that element and it's data, which isn't needed unless you actually intend to use .data() multiple times or need the extra features provided by .data()

Does UIWebView allow XML DOM Parser?

I am not quite sure, and haven't been able to find anything.
Using stringByEvaluatingJavascriptFromString:, I have been able to manipulate javascript in my page from objective-c. However, I now want to populate form fields by passing raw xml data from the iOS UIWebView into the html file (which is local to the app), and then using the parsed data.
Looking over the W3C document, it seems I need to do something like:
parser = new DOMParser();
xmlString = parser.parseFromString(txt, "text/xml");
Which should return a DOM object from the XML (which here is represented by the string txt). I should then be able to access the properties of this DOM object from
xmlString.getElementsByTagName("from")[0].childNodes[0].nodeValue;
Assuming we have an XML node such as:
<from>Sender</from>
However, this doesn't seem to work. Setting that nodeValue into a string and returning it returns nil. Likewise, form fields are not populated.
My question, then, is whether the embedded browser in an app can utilize the DOM Parser - and if it can, what syntax I might use to access values from it?
Solved, unfortunately very fast. It seems that when setting form fields you need to specify VALUE instead of innerhtml.
so I did:
document.getElementById("FIELDID").value =
xmlString.getElementsByTagName("from")[0].childNodes[0].nodeValue;
Also, I had the parameters passed to the DOM parser wrong - it is "text/xml" for the second parameter.

Categories

Resources