unidox Posted August 4, 2008 Share Posted August 4, 2008 I was wondering if there was a function I can put in the head of my website, that replaces all the & with & and all the other xhtml fixes. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/118098-replace/ Share on other sites More sharing options...
lemmin Posted August 4, 2008 Share Posted August 4, 2008 You could use regex on the innerText of the body tag. http://msdn.microsoft.com/en-us/library/9dthzd08(VS.85).aspx http://www.regular-expressions.info/javascript.html Quote Link to comment https://forums.phpfreaks.com/topic/118098-replace/#findComment-607683 Share on other sites More sharing options...
unidox Posted August 4, 2008 Author Share Posted August 4, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/118098-replace/#findComment-607708 Share on other sites More sharing options...
KevinM1 Posted August 4, 2008 Share Posted August 4, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/118098-replace/#findComment-607726 Share on other sites More sharing options...
unidox Posted August 4, 2008 Author Share Posted August 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118098-replace/#findComment-607729 Share on other sites More sharing options...
lemmin Posted August 4, 2008 Share Posted August 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118098-replace/#findComment-607737 Share on other sites More sharing options...
KevinM1 Posted August 4, 2008 Share Posted August 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118098-replace/#findComment-607740 Share on other sites More sharing options...
KevinM1 Posted August 4, 2008 Share Posted August 4, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/118098-replace/#findComment-607743 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.