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?

Link to comment
Share on other sites

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
Share on other sites

Try it like this:

<html>
<head>
<script language="JScript">
function doReplace()
{
e = document.getElementsByTagName("body")[0];
e.innerText = e.innerText.replace(/&/gm, "&");
}
</script>
</head>
<bodY onload="doReplace()">
&
&
&
</body>
</html>

I assume you only have one body object.

Link to comment
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
Share on other sites

EDIT: got it to work in Firefox:

<html>
<head>
<script type="text/javascript">
window.onload = function()
{
e = document.getElementsByTagName("body")[0];
e.innerHTML = e.innerHTML.replace(/&/g, "&");
}
</script>
</head>
<body>
&
&
&
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.