WebView2 - Send Script with a Value
-
I'm using WebView2 to display data in my app. If I send a string literal it works fine. But if I send a value in a variable I get nothing. This works:
wb.ExecuteScriptAsync("document.body.innerHTML=" & ChrW(39) & "HELLO" & ChrW(39))
This doesn't ("lStr" is my string variable):
wb.ExecuteScriptAsync("document.body.innerHTML=" & ChrW(39) & lStr & ChrW(39))
I assume I'm doing something wrong here. Any advice is appreciated.
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
I'm using WebView2 to display data in my app. If I send a string literal it works fine. But if I send a value in a variable I get nothing. This works:
wb.ExecuteScriptAsync("document.body.innerHTML=" & ChrW(39) & "HELLO" & ChrW(39))
This doesn't ("lStr" is my string variable):
wb.ExecuteScriptAsync("document.body.innerHTML=" & ChrW(39) & lStr & ChrW(39))
I assume I'm doing something wrong here. Any advice is appreciated.
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
That's going to depend on the content of your variable, which we can't see. For example, if the variable contains a single quote or any newline characters, your script will end up with an "unterminated string literal" error.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
That's going to depend on the content of your variable, which we can't see. For example, if the variable contains a single quote or any newline characters, your script will end up with an "unterminated string literal" error.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Could possibly contain quotes but no newline chars. I was trying to give my old spell checker a facelift. I wanted to use the WebView2 for the error text ("Not In Dictionary"). It's one line from the text editor with the error highlighted. So I'd need to send it the error start/end positions plus tell it to scroll to that position. All easily done if I could figure out how to send it a value in a variable.
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
-
Could possibly contain quotes but no newline chars. I was trying to give my old spell checker a facelift. I wanted to use the WebView2 for the error text ("Not In Dictionary"). It's one line from the text editor with the error highlighted. So I'd need to send it the error start/end positions plus tell it to scroll to that position. All easily done if I could figure out how to send it a value in a variable.
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.
As I said, you'll need to ensure the value is properly encoded. Assuming you're running .NET Framework 4.7.2 or later, .NET Core, or .NET 5+, then you can use the
System.Text.Json
package[^] to encode the string:wb.ExecuteScriptAsync("document.body.innerHTML=" & System.Text.Json.JsonSerializer.Serialize(lStr))
For older frameworks, use the Json.NET[^] package.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
As I said, you'll need to ensure the value is properly encoded. Assuming you're running .NET Framework 4.7.2 or later, .NET Core, or .NET 5+, then you can use the
System.Text.Json
package[^] to encode the string:wb.ExecuteScriptAsync("document.body.innerHTML=" & System.Text.Json.JsonSerializer.Serialize(lStr))
For older frameworks, use the Json.NET[^] package.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you!
Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.