Note that there are some explanatory texts on larger screens.

plurals
  1. POStripping whitespace from a JavaScript "copy-clipboard" function for Chrome/Safari
    primarykey
    data
    text
    <p>I have a copy-clipboard icon on a website, which, when clicked, invokes the following JavaScript (among other):</p> <pre><code>var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false; var elem = document.getElementById(codeID); var content; // strip out all html tags and just get the text if (!hasInnerText) { content = elem.textContent; content = content.replace(/\xA0/g,' '); } else { // ie check content = elem.innerText; } </code></pre> <p>This works fine in IE and Firefox, but not in Safari and Chrome. I assume this has something to do with their "Webkit" nature. What ends up happening is that I'm getting malformed whitespaces. I can't explain it any other way. The copy-clipboard is for code samples, and in IE and Firefox, copied code compiles. In Safari/Chrome, it does not.</p> <p>My assumption is that the <code>content.replace</code> is not being done correctly. Is there another, more appropriate RegEx that I should be doing?</p> <p>Thanks in advance!</p> <p>EDIT: I fixed the problem. There's a VERY peculiar issue which I can't believe has gone unnoticed for Chrome/Safari (so maybe something on my end?).</p> <p>I tried doing a simple alert:</p> <pre><code>// strip out all html tags and just get the text if (!hasInnerText) { content = elem.textContent; alert(content); content = content.replace(/&amp;nbsp;/g,' '); } </code></pre> <p>Which for some bloody reason wouldn't work in Chrome/Safari. BUT, when I pulled that alert outside of the if statement, it DID work. Following this line of thought, I pull the <code>content.replace</code> outside as well, and lo and behold, it worked. My code is stripping whitespace as expected.</p> <p>Someone should explain to me how this is, or if it's a known issue. Or maybe the right persons to contact.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload