Odpowiednik funkcji htmlspecialchars znanej z PHP.
var htmlspecialchars = function(html){
return [
[/&/g, '&'],
[/</g, '<'],
[/>/g, '>'],
[/"/g, '"'],
[/'/g, ''']
].reduce(function(h, r){
return h.replace(r[0], r[1]);
}, html);
};
var htmlspecialchars = function(html){
return [
[/&/g, '&'],
[/</g, '<'],
[/>/g, '>'],
[/"/g, '"'],
[/'/g, ''']
].reduce(function(h, r){
return h.replace(r[0], r[1]);
}, html);
};
Funkcja przydatna np. do umieszczania kodu na tym blogu. Przykład użycia:
prompt('skopiuj kod HTML', htmlspecialchars(prompt('podaj kod HTML')));
prompt('skopiuj kod HTML', htmlspecialchars(prompt('podaj kod HTML')));
Brak komentarzy:
Prześlij komentarz