The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

2 Pages V < 1 2  
Reply to this topicStart new topic
> string.replace against html symbol code (&divide)?
Christian J
post Jan 10 2021, 03:18 PM
Post #21


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(pandy @ Jan 10 2021, 10:25 AM) *

cool.gif cool.gif cool.gif cool.gif cool.gif cool.gif cool.gif

HTML
<p id='target'>A &divide; 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 &divide; 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?unsure.gif


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 10 2021, 03:19 PM
Post #22


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(pandy @ Jan 10 2021, 11:12 AM) *

Still want my gold star. angry.gif


You can have as many as you like, pandy. biggrin.gif

🌟 🌟 🌟
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jan 10 2021, 03:30 PM
Post #23


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,716
Joined: 9-August 06
Member No.: 6



QUOTE(Christian J @ Jan 10 2021, 09:18 PM) *


Strange, this does not work:

CODE
<p id='target'>A &divide; 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. cool.gif
https://brajeshwar.github.io/entities/


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 10 2021, 04:16 PM
Post #24


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(pandy @ Jan 10 2021, 09:30 PM) *

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. cool.gif
https://brajeshwar.github.io/entities/

Seems I already had that page bookmarked, we must have been discussing this before.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jan 10 2021, 04:46 PM
Post #25


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,716
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... blush.gif

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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 10 2021, 07:56 PM
Post #26


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



CSS generated content? Maybe I saw that page when reading this: https://mathiasbynens.be/notes/css-escapes unsure.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jan 10 2021, 08:10 PM
Post #27


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,716
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. unsure.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

2 Pages V < 1 2
Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th March 2024 - 10:56 AM