Reputation: 4435
I have a WebBrowser
control on a WinForm form, which should show a specific HTML page. Within the HTML page, I have some <template>
s that should be used to populate the page. When I use the following code:
string template = webBrowser.Document.GetElementById("template-id").InnerHtml
I get null
, instead of the actual inner HTML.
I guess it has to do with the fact that the template
tag is not supported in old IE versions, hence in the WebBrowser
control. How can I fix it? Is it even possible?
Of course, I could put my template into a string and be done with it, but this seems very hacky.
Thanks!
Upvotes: 0
Views: 261
Reputation: 125197
WebBrowser control uses Internet Explorer and Internet Explorer doesn't support <template>
tag. (See Browser compatibility) section.
If you can modify content of pages, there are some workaround for you to be able to use <template>
tag. You can use either of the following options:
Option 1 - Make the tag hidden and load IE in edge mode
webBrowser1.DocumentText = @"
<html>
<head>
<title>Test</title>
<meta http-equiv=""X-UA-Compatible"" content=""IE=Edge"" />
</head>
<body>
<h1>Test</h1>
<div>This is a test page.</div>
<template id=""template1"" style=""display:none;"">
<h2>Flower</h2>
<img src=""img_white_flower.jpg"" />
</template>
</body>
</html>";
webBrowser1.DocumentCompleted += (obj, args) =>
{
var element = webBrowser1.Document.GetElementById("template1");
MessageBox.Show(element?.InnerHtml);
};
Option 2 - Include The HTML5 Shiv in the page
webBrowser1.DocumentText = @"
<html>
<head>
<title>Test</title>
<!--[if lt IE 9]>
<script src=""https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"">
</script>
<![endif]-->
</head>
<body>
<h1>Test</h1>
<div>This is a test page.</div>
<template id=""template1"">
<h2>Flower</h2>
<img src=""img_white_flower.jpg"" />
</template>
</body>
</html>";
webBrowser1.DocumentCompleted += (obj, args) =>
{
var element = webBrowser1.Document.GetElementById("template1");
MessageBox.Show(element?.InnerHtml);
};
If you cannot modify content of the page, you need to use a different browser control, for example:
Upvotes: 1