locked
How can I make WebView non-selectable?

    Question

  • I have a WebView showing some copyrighted material that I don't want the user to be able to select and copy to the clipboard.

    Is there some way to disable the selectability of text shown in a WebView control? Even if it is a hack as long as it works. The WebView still needs to be scrollable though, so I don't just want to disable all touch input on it.

    BTW this is a XAML application for Windows 8 Store.


    \^_^/



    Monday, May 12, 2014 5:16 PM

Answers

  • Hi, it is WebView, so you can modify the HTML file to disable the selectability.

    For example, you can try to add below script to your html file:

    <html onselectstart="return false;" style="-ms-user-select: none;">
    ...
    </html>

    You can try to add a webview and use below code to test it:

    webView.NavigateToString("<html onselectstart=\"return false;\" style=\"-ms-user-select: none;\"><body><h2>This is an HTML fragment</h2></body></html>");


    Tuesday, May 13, 2014 6:44 AM
  • Hi zenanimator,

    If you are working with win8.0 app, probably WebViewBrush class is a good choice for you.

    And as lapheal suggested, a JavaScript way could also be a solution, however what he suggested is for Firefox, Windows Store App WebView use IE render engine, so probably you should use "-ms" as the prefix.

    I would suggest another JS way to disable the selection, which is:

    document.onselectstart = function() { return false; }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, May 13, 2014 7:16 AM
    Moderator
  • Hi, thanks that worked! (Bit of a pain to add them to all my html pages, but at least it works)

    I can use either of these they both seem to work: (Any reason why I need both?)

    <html style="-ms-user-select: none;">

    or

    <html onselectstart="return false;">


    \^_^/


    Hi, they are different, the first one is CSS style, the second one is JavaScript, so it will be safe if you use both, because sometimes CSS may not supported correctly by the browser and sometimes JavaScript may be disabled.

    And it is a mistake in my first post, it should be "-ms-user-select: none;", thanks Jamles to correct it.

    Wednesday, May 14, 2014 3:25 AM

All replies

  • Hi, it is WebView, so you can modify the HTML file to disable the selectability.

    For example, you can try to add below script to your html file:

    <html onselectstart="return false;" style="-ms-user-select: none;">
    ...
    </html>

    You can try to add a webview and use below code to test it:

    webView.NavigateToString("<html onselectstart=\"return false;\" style=\"-ms-user-select: none;\"><body><h2>This is an HTML fragment</h2></body></html>");


    Tuesday, May 13, 2014 6:44 AM
  • Hi zenanimator,

    If you are working with win8.0 app, probably WebViewBrush class is a good choice for you.

    And as lapheal suggested, a JavaScript way could also be a solution, however what he suggested is for Firefox, Windows Store App WebView use IE render engine, so probably you should use "-ms" as the prefix.

    I would suggest another JS way to disable the selection, which is:

    document.onselectstart = function() { return false; }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, May 13, 2014 7:16 AM
    Moderator
  • Hi, thanks that worked! (Bit of a pain to add them to all my html pages, but at least it works)

    I can use either of these they both seem to work: (Any reason why I need both?)

    <html style="-ms-user-select: none;">

    or

    <html onselectstart="return false;">


    \^_^/


    Tuesday, May 13, 2014 5:43 PM
  • Hi, thanks that worked! (Bit of a pain to add them to all my html pages, but at least it works)

    I can use either of these they both seem to work: (Any reason why I need both?)

    <html style="-ms-user-select: none;">

    or

    <html onselectstart="return false;">


    \^_^/


    Hi, they are different, the first one is CSS style, the second one is JavaScript, so it will be safe if you use both, because sometimes CSS may not supported correctly by the browser and sometimes JavaScript may be disabled.

    And it is a mistake in my first post, it should be "-ms-user-select: none;", thanks Jamles to correct it.

    Wednesday, May 14, 2014 3:25 AM