1

Javascript / HTML Code

<script type="text/javascript">
function configurator(clicked) { 
return clicked.name;   
} 
</script>

<a name="link1" href="#" onclick="configurator(this)">Link 1</a>
<a name="link2" href="#" onclick="configurator(this)">Link 2</a>

I want to return the "clicked.name" value to let's say "result" variable in C# when onclick event on the link triggered. I don't know how to do that. Could someone help me...

Note: I'm still very new to Awesomium :)

Update:

I follow @JonnyReeves method with a little modified:

using (JSObject myGlobalObject = webControl1.CreateGlobalJavascriptObject("myGlobalObject"))
{
    myGlobalObject.Bind("onLinkClicked", true, (sen, eve) =>
    {
        MessageBox.Show(Convert.ToString(sen));
    });
}

but I got "Awesomium.Windows.Controls.WebControl" as result not the clicked link's name.

3
  • Ajax. It is not possible to simply return data to the server, but you can make a separate request and pass the data along with it. Commented Mar 24, 2013 at 5:32
  • I'm not using any web server. I'm just using Awesomium (WebControl) as browser. I still learning how to exchanging data between C# and html (Awesomium). Commented Mar 24, 2013 at 5:45
  • Duplicate of accepted answer... : stackoverflow.com/questions/8089912/… Commented Feb 23, 2014 at 15:40

1 Answer 1

0

It looks like you you could make use of Awesomium's JSObject.bind method to invoke a C# method from JavaScript. A typical approach would be to expose a JavaScript global object which includes all your 'bridge' methods (ie: those that pass data between C# and JavaScript).

// Create and acquire a Global Javascript object.
// These object persist for the lifetime of the web-view.
using ( JSObject myGlobalObject = webView.CreateGlobalJavascriptObject( "myGlobalObject" ) )
{
    // The handler is of type JavascriptMethodEventHandler. Here we define it
    // using a lambda expression.
    myGlobalObject.Bind( "onLinkClicked", false, ( name ) =>
    {
        Debug.Print( String.Format( "User clicked: {0}", name ) );
    } );
}

You can now invoke this method from your JavaScript code:

<script type="text/javascript">
    function configurator(clicked) 
    { 
           myGlobalObject.onLinkClicked(clicked.name);
    } 
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your code but got error on "Delegate 'Awesomium.Core.JavascriptMethodEventHandler' does not take 1 arguments" so I changed your code a little (see OP). But I can't get the clicked link's name (<a name="link1">).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.