Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 536
Rediet Avatar asked Jun 21 '26 03:06

Rediet


1 Answers

Since you're creating an a element, you simply assign the href to the element via the .href property.

You can set its text content with .innerHTML as a convenience, though it's not really a "pure" approach.

var img11=document.createElement("a");

img11.href='http://google.de';
img11.innerHTML = 'Google';

document.body.appendChild( img11 );

Another way to set the text content would be like this...

img11.appendChild(document.createTextNode("Google"));
like image 144
user1106925 Avatar answered Jun 23 '26 17:06

user1106925