View Full Version : i need a script to convert html codes
um...
08-17-2002, 10:05 PM
:confused: heya ... :confused:
for one of my pages on my website (ChatDot (http://chatdot.tripod.com) ) i need a javascript which will, when the page loads, go through the html in the page, and convert all the html codes for characters, into there actual characters.
For example, in some html it says
<, but i want it to be "<"
does anyone know how to do this?
if so, please write tell me...
thanks in advance...
That shouldn't be difficult, but there are a large number of entities (the proper name for what you are calling codes). I would suggest you create an object with all the entity names and their replacement character like this:
var entities = {
lt: '<',
gt: '>',
amp: '&',
apos: '\'',
quot: '"'
}
Then use a function to loop through this object replacing all entities. You could adapt the following code to do it:
var re = new RegExp( ,'g');
for (entitiy in entities) {
re.source = '&' + entity + ';';
txt.replace(re, entities[entity]);
}
txt is of course the text in which you want to replace the entities with their values. I am not sure of the setup of your page so you will have to figure out that bit yourself or post more info.
beetle
08-17-2002, 11:46 PM
Mind if I but in? I'd like to inject a question of my own...
dsvg...did you just create an array hash? I was under the impression that those didn't exist in javascript? Cool cool cool. Oh, and BTW, the apos needs escaping....var entities = {
lt: '<',
gt: '>',
amp: '&',
apos: '\'',
quot: '"'
}
Wierd! I am sure I escaped that apostrophy. :confused:
With regards to array hashes, they don't exist as they are defined in 'reall' programming languages. In JavaScript everything is a property of an object. Even your global variables are properties of the window object.
There are two ways to access a property in JavaScript. You can either use:
window.x
or
window['x']
The . and [] are both member operators. The difference is that . takes the variable name as a JavaScript lable, whereas [] takes the variable name as a string. You may have seen it used this way before in the for loop:
for (prop in obj) {
alert(prop + ' = ' + obj[prop]
}
The prop in this case takes the name of each property belonging to obj in turn as a string. Hence why you have to use the [] member operators.
Going back to hash arrays you can add and access properties to and from an array using the [] operators. But they only behave as properties. You don't get the linking between the array elements and the hash as you do in a real hash array.
I hope that answers your question. :)
beetle
08-18-2002, 10:44 PM
yes, it does. And I have used for loops like that, I had just never seen the assignment notation you used (with the colon) I'm rougly equating this to PHP's =>. Thanks for the clarification.
Oh, and I realized why I didn't see the backslash you put in. For it to show in the forum, you need to escape your escaping backslash with yet another backslash (whew!) :D
Ah, I see. Thanks for letting me know. I will remember to escape my backslashes from now on. Thanks. :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.