string.replace against html symbol code (÷)? |
string.replace against html symbol code (÷)? |
Christian J |
Jan 10 2021, 03:18 PM
Post
#21
|
. Group: WDG Moderators Posts: 9,658 Joined: 10-August 06 Member No.: 7 |
HTML <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> CODE function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('\367', '/'); document.getElementById('target').innerHTML = replacestring; } Strange, this does not work: CODE <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> <script type="text/javascript"> function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('÷', '/'); // Note that I don't use an entity here. document.getElementById('target').innerHTML = replacestring; } </script> --why isn't the entity "interpreted" now? Is the replace() method different? |
Christian J |
Jan 10 2021, 03:19 PM
Post
#22
|
. Group: WDG Moderators Posts: 9,658 Joined: 10-August 06 Member No.: 7 |
|
pandy |
Jan 10 2021, 03:30 PM
Post
#23
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,731 Joined: 9-August 06 Member No.: 6 |
Strange, this does not work: CODE <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> <script type="text/javascript"> function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('÷', '/'); // Note that I don't use an entity here. document.getElementById('target').innerHTML = replacestring; } </script> We already knew that. CODE --why isn't the entity "interpreted" now? Is the replace() method different?:unsure: And that too. It is interpreted, but it doesn't work to use the actual character for the replace. Those too things are what was so darn odd! I was googling html entities and javascript and stumbled on this page. Then I got the idea that maybe if one used the character code JS prefers... And it worked. https://brajeshwar.github.io/entities/ |
Christian J |
Jan 10 2021, 04:16 PM
Post
#24
|
. Group: WDG Moderators Posts: 9,658 Joined: 10-August 06 Member No.: 7 |
I was googling html entities and javascript and stumbled on this page. Then I got the idea that maybe if one used the character code JS prefers... And it worked. https://brajeshwar.github.io/entities/ Seems I already had that page bookmarked, we must have been discussing this before. |
pandy |
Jan 10 2021, 04:46 PM
Post
#25
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,731 Joined: 9-August 06 Member No.: 6 |
Nope. I didn't know about this and I hadn't seen that page. Well, I knew that kind of octal codes could be used in JS, but I didn't know they needed to be used in replace strings.
I may add that I still don't understand why they need to be used... In a case like this the ampersand doesn't need to be escaped. What's the difference? CODE var test = 'this & that'; alert(test); Are there other cases when these characters need escaping? |
Christian J |
Jan 10 2021, 07:56 PM
Post
#26
|
. Group: WDG Moderators Posts: 9,658 Joined: 10-August 06 Member No.: 7 |
CSS generated content? Maybe I saw that page when reading this: https://mathiasbynens.be/notes/css-escapes
|
pandy |
Jan 10 2021, 08:10 PM
Post
#27
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,731 Joined: 9-August 06 Member No.: 6 |
Haven't seen that page either. The only things I've read about CSS escape characters is in the spec and maybe some book, I think.
|
Lo-Fi Version | Time is now: 26th April 2024 - 02:24 PM |