sábado, 30 de noviembre de 2013

Insertar caracteres especiales en Javascript

Las cadenas de escape más conocidas son:
  • \n: Salto de línea
  • \r: Retorno de carro
  • \t: Tabulación horizontal
  • \v: Tabulación vertical
  • \’: Comilla simple
  • \”: Comilla doble
  • \\: Barra invertida
  • \xdd: Carácter especial especificado mediante dos dígitos hexadecimales dd
El último caso, el del carácter especial (\xdd), es el que nos permite, entre otras cosas, insertar letras acentuadas o la letra ñ.
Lo único que necesitamos para ponerlo en práctica es una tabla con el juego de caracteres unicode. Veamos la tabla para latin:
image

Insertar un fichero HTML dentro de un documento HTML

Objetivo: 

Insertar el fichero b.html dentro del documento a.html

Solución:

a.html:
<html> 
  <body>
  <h1>Put here your HTML content before insertion of b.js.</h1>
      ...

  <script src="b.js"></script>

      ...

  <p>And here whatever content you want afterwards.</p>
  </body>
</html>
 
b.js:
document.write('\
\
    <h1>Add your HTML code here</h1>\
\
     <p>Notice however, that you have to escape LF's with a '\', just like\
        demonstrated in this code listing.\
    </p>\
\
');

The reason for me against using jQuery is that jQuery.js is ~90kb in size, and I want to keep the amount of data to load as small as possible.

OTRA OPCION

No need for scripts. No need to do any fancy stuff server-side (tho that would probably be a better option)
 
<iframe src="/path/to/file.html" seamless></iframe>