create link in javascript function with --- innerHTML = "...<a href='http://...'>HttpLink</a>..."

12,308

You code is creating another HTML page inside the original. That is wrong and invalid. It's because some browsers are forgiving and correcting things that your code even works.

If you're creating a hyperlink element, you should be adding text or an image or other inline elements inside it using innerHTML.

You should change its attribute to set the href with img11.href='http://xyz.com';

Share:
12,308
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm learning to write with html, but I have one problem I cannot understand. The following code works in other browsers but not in IE9. I know it has something to do with innerHTML but I could not understand the answers I found for this.

    <html> <head> <script type="text/javascript">
    function hw_function1() {
        var img11=document.createElement("a"); 
        img11.innerHTML = "<html> <body> <a href='http://google.de'>Google</a> </body></html>";
        document.body.appendChild( img11 );
    }
    </script>
    <body>
        <a href="#" onclick="javascript:hw_function1()";>Test</a>
    </body> </html>
    

    What should I change WITHOUT changing the structure (only the innerHTMl-part if possible)?