2

Commenting on this question made me wonder.

In JavaScript, is it possible to get the "raw" value of a HTML attribute, i.e. the way it was written in the source before parsing?

Say you have this HTML:

<section id="theSection" title="The&#32;section">The section</section>

<script>
 console.log(document.getElementById('theSection').title);
</script>
 

What do I need to write in the script to make it output the original escaped value of the title, rather than the parsed one?
JavaScript nodes have a lot of properties and values, but in this case, none that say "The&#32;section" instead of "The section".

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • 1
    That could only be done using String parsing on the HTML you get via an asynchronous GET request. Obviously the DOM API cannot access elements not in the DOM, and as soon as an element is in the DOM, it has obviously been parsed. – connexo Nov 22 '18 at 20:30
  • Ah yes, I don't want to to that. At best, downloading the same HTML file again and having to search through it; at worst, re-invoking the web app, with who knows what side effects. No, no. – Mr Lister Nov 22 '18 at 20:46

1 Answers1

0

There is no way to get parsed Unicode charachter in the browser like source HTML file and as &#32; is a space then we can use this workaround for any title has space between its words.

<section id="theSection" title="The&#32;section">The section</section>

<script>
 console.log(document.getElementById('theSection').title.split(' ').join('&#32;'));
</script>
 

I know it is silly answer, but if really intended to process space unicode then hit the url which has this <section id="theSection" title="The&#32;section">The section</section> with fetch(url) like below:

fetch('stackoverflow.com')
  .then(async (res) => {
    const text = await res.text();
   // process the text with any unicode charachter as it is now 100% string 
  }
);

Now the source HTML file is a string. It is better to use in your case javascript on string with a regex to find what do you want to process in html source.

Poode
  • 1,692
  • 1
  • 15
  • 20