How to read empty XML elements? - javascript

<address>111 North Bridge Road </address>
Hi,
I have a XML file with many tags like the above.
I'm using javascript that read the file and use document.write to write contents of the element.
However everytime it reads an empty element, it stops writing .
Is there anyway to check for an empty element using javascript?

An empty element doesn't have childNodes, so you could check
element.childNodes.length

Related

MathJax string input/output

I've just learned about math MathJax. I've read some documentation, and it looks nice but If I understood it right it scans html page for equations and automatically replace them with HTML/SVG or whatever.
My concern is that I'm generating my page from raw text (simply strings) in JS and I would like to just simply pass certain parts of it to MathJax and get html string in return. I looked at the API but unless I'm missing something, there is no such option and I can only do things like MathJax.Hub.Process().
So it would look like
I'm creating and adding HTML elements to document based on my text.
I'm telling MathJax to search for the content I've just created.
MathJax is searching for text to parse and removing the content I've just added, striping out text, parsing....
MathJax is creating it's own elements add it to document.
Is there a way (without modifying the source code) to do something like this:
I'm passing text to MathJax, telling it what is the format.
MathJax is returning HTML text string.
I'm creating HTML element besed on that text and inserting it at desired place.
I hope I overlooked something.

Convert RTF to HTML in Javascript

Is there a way to convert RichTextFormat to HTML in Javascript?
Iam trying to paste the RTF content copied , from clipboard iam getting the text/rtf content, now i need to show it with all styles applied in a div. how can i achive this ?? any suggestions?.
Eg:
if i copy
ghkasjhdk
gjhgjh
^^ this as RTF
i will get a string simmilar to this
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\b\f0\fs40\par
\b0\fs22 ghkasjhdk\par
\b\fs40 gjhgjh}
i need this to be converted to html with <strong> tags and <br>'s respectively
You may wish to adapt this NodeJS package: rtf2html.
I understand this has already be done with an older version, as you can see in this Github repository.
After you get the corresponding HTML code, you can add it to your container using the usual DOM methods, or a library like jQuery if you are already using it.

Using JavaScript to scan dynamically rendered elements?

I apologize in advance if I've formulated this question poorly; I'm fairly new to web coding.
My goal is to use JavaScript to scan a webpage and determine whether or not a particular string is present. The difficulty here is that the page is dynamically rendered, so the string in question will never appear in the source code.
Would the string appear in the DOM if it is rendered on the page? If I were to scan the DOM would I find it there, and are there any special considerations to take into account if so?
Essentially I am looking for a simple way to scan the text that has been rendered on a page, not the source code. This must be possible somehow because my browser's "find on page" function works on the dynamically rendered page in question. Would there be a way to access the rendered elements on the page through the browser API itself? (I'm using Chrome.)
Try this:
var stringToSearchFor = 'foobar';
var searchThisString = document.body.innerText || document.body.textContent;
var found = (searchThisString.indexOf(stringToSearchFor) >= 0);
This extracts the text from the page, ignoring all markup, then does a simple scan of the resulting string.
In some versions of FireFox this will include the contents of inline script tags.

how to pass as parameter to JS function from HTML large chunk of HTML code?

I want to pass as argument a large (maybe 2-3 paragraphs of html formatted code) chunk of HTML code to a Javascript function call from HTML. The problem is, the formatted HTML keeps appearing in the page itself, which shouldnt be the case ! I am assuming theres some problem with single/double quotes !
And, I am working on Facebook tab page.
Can anyone please help me ?
Thanks.
-
ahsan
One way is to have a hidden div (something with display:none), and populate that with your 2-3 paragraphs of html formatted code. Then, you can just pass the innerHTML of the div into your function. Quotes (of any kind) won't cause a problem in this method.
Some libraries like icanhaz.js also do something like this:
<script type="text/html" id="someHTMLTemplate">
<div>You can put whatever html you want here</div>
<p>And the browser just ignores it</p>
</script>
I use the same technique with mustache.js and then grab the template from the innerHTML of the script tag after grabbing it by the dom id. This has the advantage that the browser doesn't have to parse your extra html while loading it is just parsed when you need to display it in another node on the page.
Another way is to encode the HTML and then decode it in the JS. Here's an example using the JS escape info:
console.log(escape("<hello></hello>")); // %3Chello%3E%3C/hello%3E
console.log(unescape("%3Chello%3E%3C/hello%3E")); // <hello></hello>
Mind you, if you have an issue with your string quotations to begin with, then there will still be a problem encoding.

Possible to create custom "DOMs" by loading HTML from string in Javascript?

I'm trying to parse HTML in the browser. The browser receives 2 HTML files as strings, eg. HTML1 and HTML2.
I now need to parse these "documents" just as one would parse the current document. This is why I was wondering if it is possible to create custom documents based on these HTML strings (these strings are provided by the server or user).
So that for example the following would be valid:
$(html1Document).$("#someDivID")...
If anything is unclear, please ask me to clarify more.
Thanks.
var $docFragment = $(htmlString);
$docFragment.find("a"); // all anchors in the HMTL string
Note that this ignores any document structure tags (<html>, <head> and <body>), but any contained tags will be available.
With jQuery you can do this:
$(your_document_string).someParsingMethod().another();
You can always append your html to some hidden div (though innerHTML or jQuery .html(..)). It won't be treated exactly as a new document, but still will be able to search its contents.
It has a few side-effects, though. For example, if your html defines any script tags, they'll be loaded. Also, browser may (and probably will) remove html, body and similar tags.
edit
If you specifically need title and similar tags, you may try iframe loading content from your server.

Categories