Does Visual Studio Code's Webview API support innerHTML?
·
Answer a question
I'm building a VSCode extension which uses webview to display dynamic data using javascript and innerHTML
. I found that the code is working properly in Chrome, but the innerHTML
section does not work in VS Code webview.
HTML code
<!DOCTYPE html>
<html>
<body>
<div id="root">
Root
</div>
<script>
document.getElementById('root').innerHTML += '<div>Inner HTML Success</div>'
</script>
</body>
</html>
Here is the extension code:
vscode.commands.registerCommand('my-extension.openFileInWebview', async (url: string) => {
const panel = vscode.window.createWebviewPanel(
'catCoding',
'Cat Coding',
vscode.ViewColumn.One,
{}
);
const filePath = path.join(context.extensionPath, 'src', 'webviews', 'innerHtml.html');
const fileContents = await readFile(filePath);
const html = fileContents.toString();
panel.webview.html = html;
});
In VS Code
In Chrome
Answers
innerHTML
is supported. Just that Javascript is disabled in webview by default, and needs to be enabled using enableScripts: true
option.
const panel = vscode.window.createWebviewPanel(
'catCoding',
'Cat Coding',
vscode.ViewColumn.One,
{
enableScripts: true
}
);
更多推荐
所有评论(0)