Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. WebView2 - Send Script with a Value

WebView2 - Send Script with a Value

Scheduled Pinned Locked Moved JavaScript
toolscareer
5 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alan Burkhart
    wrote on last edited by
    #1

    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.

    Richard DeemingR 1 Reply Last reply
    0
    • A Alan Burkhart

      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.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      A 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        A Offline
        A Offline
        Alan Burkhart
        wrote on last edited by
        #3

        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.

        Richard DeemingR 1 Reply Last reply
        0
        • A Alan Burkhart

          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.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          A 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            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

            A Offline
            A Offline
            Alan Burkhart
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups