locked
How to get a value given to a button in HTML using a href tag

    Question

  • Hi,


    In my application I have loaded  html  content  in WebView. In the HTML file i have used a Button to which i have given some value in href tag as

    <a href="ButtonClick//testing/mysample">
       <input type="button" value="Click me" />
    </a>

    When i click the button i want to get the string "ButtonClick//testing/mysample" in my app..  But i do not want to use InvokeScript() on my webview.. Could anyone please give me a solution for this?..

    Thank you.

    Monday, February 9, 2015 1:52 PM

Answers

  • The only way to interact with HTML content from WebView is using JavaScript. You can do this by invoking such scripts from your C# code (using InvokeScript) or embedding the JavaScript method into the button you are working with.

    But you need to know that if you intend to get a return value from your JavaScript code, you would need to use the InvokeScript.

    Here's an example of printing the value from your hyperlink without InvokeScript. But note that it only prints the value and can't return the value to your C# code without InvokeScript.

    string htmlContent = @"
                <html>
                    <head>
                        <script type='text/javascript'>
                            function myFunction()
                            {
                                document.getElementById('myResult').innerHTML = document.getElementById('myLinkConent').href.substring(6); //remove 'about:' from href string
                            }
                        </script>
                    </head>
                    <body>
                        <a id='myLinkConent' href='ButtonClick//testing/mysample'>
                           
                        </a>     
                        <input type='button' value='Click me' onclick='myFunction()'/>
            
                        <p id='myResult'></p>               
                     </body>
                </html>";

    Let me know if this helps.


    Abdulwahab Suleiman

    • Proposed as answer by Magnus (MM8)MVP Wednesday, February 11, 2015 2:05 PM
    • Marked as answer by PM16 Wednesday, February 11, 2015 2:08 PM
    Tuesday, February 10, 2015 1:40 AM
    Moderator

All replies

  • The only way to interact with HTML content from WebView is using JavaScript. You can do this by invoking such scripts from your C# code (using InvokeScript) or embedding the JavaScript method into the button you are working with.

    But you need to know that if you intend to get a return value from your JavaScript code, you would need to use the InvokeScript.

    Here's an example of printing the value from your hyperlink without InvokeScript. But note that it only prints the value and can't return the value to your C# code without InvokeScript.

    string htmlContent = @"
                <html>
                    <head>
                        <script type='text/javascript'>
                            function myFunction()
                            {
                                document.getElementById('myResult').innerHTML = document.getElementById('myLinkConent').href.substring(6); //remove 'about:' from href string
                            }
                        </script>
                    </head>
                    <body>
                        <a id='myLinkConent' href='ButtonClick//testing/mysample'>
                           
                        </a>     
                        <input type='button' value='Click me' onclick='myFunction()'/>
            
                        <p id='myResult'></p>               
                     </body>
                </html>";

    Let me know if this helps.


    Abdulwahab Suleiman

    • Proposed as answer by Magnus (MM8)MVP Wednesday, February 11, 2015 2:05 PM
    • Marked as answer by PM16 Wednesday, February 11, 2015 2:08 PM
    Tuesday, February 10, 2015 1:40 AM
    Moderator
  • Thank you.. It helps me lot :)
    Wednesday, February 11, 2015 12:52 PM