Jump to content

Replace


unidox

Recommended Posts

I tried this:

 

<script language="Javascript" type="text/javascript">
document.prototype.htmlEntities = function () {
   return this.replace(/&/g,'&');
};
</script>

 

and then in body I did this:

 

<body onload="htmlEntities()">

 

But it doesnt work. Any ideas?

 

I believe you need to get your hands on the actual text nodes in question.

 

Is there any reason why you're not doing this manually?  Or simply not doing something like:

function htmlEntities(text)
{
   return text.replace(/&/g, '&');
}

.
.
.

var myBody = document.getElementsByTagName("body")[0];
var myP = document.createElement("p");
var myText = document.createTextNode("This is text to be appended to a paragraph and to test the '&' symbol.");
myText = htmlEntities(myText);

myP.appendChild(myText);
body.appendChild(myP);

Link to comment
https://forums.phpfreaks.com/topic/118098-replace/#findComment-607726
Share on other sites

Well, this cant be done manually due to its a dynamic site. All I am trying to do is replace & with & inside the html of my site.

 

Dynamic in which way?

 

I mean, if you're using PHP to store/display pages, then scrub user input with htmlEntities before saving it to the DB/file.  Or, at the very least, run htmlEntities before outputting it to the screen.

 

If this is supposed to be for validation purposes, the site may not validate if you use JavaScript for this, as I'm not sure if the script actually changes the markup, or merely changes how it's rendered.  So, the validator may still see the actual '&' characters instead of their related entities.

Link to comment
https://forums.phpfreaks.com/topic/118098-replace/#findComment-607740
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.