Results 1 to 5 of 5

Thread: Single quotes make my code hover

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2022
    Posts
    27

    Single quotes make my code hover

    Hello, Sorry for my English ;-)

    I encounter a new problem in my mutation from webbrowser to webview.

    I am creating variables of type string and inlining them using a webview2 to populate a field. On the other hand when I have an apostrophe (') then the text does not appear.

    Example :

    Code:
    dim variable as string = "En tant qu invité"
    works

    Code:
    dim variable as string = "En tant qu'invité"
    does not.

    I replaced tested:
    Code:
    dim variable as string = "En tant qu" & chr(39) & "invité"
    it doesn't work.

    I can't find the trick to get out of this mess for 3 hours!

    Thank you !

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Single quotes make my code hover

    I suspect this is an escaping issue with the javascript when calling ExecuteScriptAsync.

    Take this for example:
    Code:
    Dim innerHTML = "En tant qu'invité"
    webview21.ExecuteScriptAsync($"document.getElementById('my-element').innerHTML = '{innerHTML}';")
    What actually gets executed is this:
    Code:
    document.getElementById('my-element').innerHTML = 'En tant qu'invité'
    So the string ends prematurely after qu and a JavaScript error would occur, something to the effect of:
    Uncaught SyntaxError: Unexpected identifier
    What I would suggest trying is either escaping the single quote, using a different string encapsulation, or by using the HTML entity, e.g.
    Code:
    Dim innerHTML = "En tant qu\'invité"
    webview21.ExecuteScriptAsync($"document.getElementById('my-element').innerHTML = '{innerHTML}';")
    
    -or-
    Dim innerHTML = "En tant qu'invité"
    webview21.ExecuteScriptAsync($"document.getElementById('my-element').innerHTML = `{innerHTML}`;")
    
    -or-
    Dim innerHTML = "En tant qu'invité"
    webview21.ExecuteScriptAsync($"document.getElementById('my-element').innerHTML = '{innerHTML}';")
    Last edited by dday9; Jun 10th, 2022 at 03:55 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Single quotes make my code hover

    Try escaping the apostrophe...

    Code:
    dim variable as string = "En tant qu\'invité"
    Or...

    Code:
    dim variable as string = "En tant qu\\'invité"

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Single quotes make my code hover

    You can also use double-quotes as your HTML delimiters so that single-quotes in your data won't be a problem. Using dday9's example:
    vb.net Code:
    1. Dim innerHTML = "En tant qu'invité"
    2. webview21.ExecuteScriptAsync($"document.getElementById(""my-element"").innerHTML = ""{innerHTML}"";")

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2022
    Posts
    27

    Re: Single quotes make my code hover

    Thanks for your help. Replacing "qu'invité" that "qu\'invité" guest with works perfectly !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width