Tengo como requisito de un proyecto cargar el código HTML dentro de este script de texto/vista que debe mostrarse la primera vez que el usuario carga la página. ¿Cómo hago para que el navegador lea el código dentro del primer script? ¿Y cómo, por ejemplo, puedo hacer lo mismo solo para el segundo script?
<html> <head> <script type = "text/view" id ="welcomeview"> Hello World! </script> <script type = "text/view" id ="profileview"> <h4>Happy Birthday!!<h4> </script> </head> <!-- obviously not working --> <body onload="welcomeview"> </body> </html>Aquí hay un HTML5 serializado como código de ejemplo de forma XML (estricto) para que pueda aprender. Querrá buscar diferentes partes para aprender y hay muchas maneras diferentes de hacer lo mismo.
Puede modificar el código tanto como desee para averiguar cuando elimina X o Y cómo romperá la página. Nuevamente, este es un código muy estricto que es mucho mejor para aprender y simplemente usar en general porque será mucho más detallado cuando haga algo mal.
Guarde este archivo usando un editor adecuado como Notepad++ (solo Windows, no estoy seguro acerca de Mac/Linux) como example.xhtml , no use la extensión html normal. ¡Buena suerte!
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <script type="application/javascript"> //<![CDATA[ //This is a JavaScript comment, not the same as an HTML comment. //The CDATA thing helps the browser avoid code being interpreted as HTML (in short). //There is no such thing as "text/javascript" or "text/view", use "application/javascript". //Define a function that will be called: function test_function() { var example_string = '<p xmlns="http://www.w3.org/1999/xhtml">Happy birthday! This is an example of a string that LOOKS like but is not HTML.</p>'; //You MUST have the XML Namespace (xmlns) on the ROOT most element and can not have multiple root elements. var xml = new DOMParser().parseFromString(example_string,'application/xml'); //You have to import a string next: var xml = document.importNode(new DOMParser().parseFromString(example_string,'application/xml').childNodes[0],true); //Now add the element to the DOM (HTML): document.getElementsByTagName('main')[0].appendChild(xml); } //Events should always be at the END of your (JavaScript) code: window.onload = function(event) { //Call one or more functions when the page loads: test_function(); } //]]> </script> </head> <body> <main></main> <!-- This is an HTML comment, the syntax is different from JavaScript comments. --> </body> </html>