Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call JavaScript from C# - Cordova/PhoneGap

I'm using cordova/phonegap to make a windows phone app, i'm trying to call a script from C# when an event fires.

Is there anyway to do this?

here's my class so far.

public void register(string options)
{
        // This is executed asynchronously
        if (!TryFindChannel())
            DoConnect();
}


void httpChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
        // Finished asynchronous task in "register" method
        Trace("Channel opened. Got Uri:\n" + httpChannel.ChannelUri.ToString());
        SaveChannelInfo();

        Trace("Subscribing to channel events");
        SubscribeToService();
        SubscribeToNotifications();

        // SEND CHANNEL URI TO JAVASCRIPT

}
like image 648
Mike Bryant Avatar asked Nov 08 '13 16:11

Mike Bryant


People also ask

Can we call JavaScript function from code behind C?

You cannot. Codebehind is running on the server while JavaScript is running on the client.

How do I call a JavaScript script?

The first method is to call the JavaScript function in HTML. For this, you have to create a function then define this function either in the head section or body section of the HTML document. You can either create a link or a button and then an onclick() event is associated with them in order to call this function.

Can you call JavaScript in CSS?

No, you can't trigger JavaScript from CSS directly. What you can do is use CSS selectors to find the elements you want to watch in this way, and then watch for mouse events.

What is Call () in JavaScript?

The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.


2 Answers

Try:

webBrowser.InvokeScript("myFunction", "one", "two", "three");

InvokeScript executes a scripting function defined in the currently loaded document, and passes the function an array of string parameters.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402838%28v=vs.105%29.aspx

Obviously you have to have the JavaScript function defined in the loaded document.

Depending on your view it may work like this:

this.CordovaView.Browser.InvokeScript("eval", new string[] { "yourJavascriptFunction(); " });
like image 124
benka Avatar answered Oct 22 '22 10:10

benka


I found a solution, admittedly not the best one but works for me.

I created a singleton class called WebViewHandler which looks like this

class WebViewHandler
{
    private static WebViewHandler instance;
    public bool isWebViewReady { get { return webView != null; } }
    public WPCordovaClassLib.CordovaView webView;

    private WebViewHandler()
    {

    }

    public void setWebView(ref WPCordovaClassLib.CordovaView webView)
    {
        this.webView = webView;
    }

    public static WebViewHandler getInstance()
    {
        if(instance == null){
            instance = new WebViewHandler();
        }
        return instance;
    } 
}

Then I set the webview in the constructor on the HomePage like so:

 public HomePage()
 {
      InitializeComponent();
      CordovaView.Loaded += CordovaView_Loaded;
      WebViewHandler.getInstance().setWebView(ref CordovaView); 
 }

Once the WebView is set I can then call InvokeScript from any other class:

WebViewHandler.getInstance().webView.CordovaBrowser.InvokeScript("MyJavaScriptFunctionThatIWishToCall");
like image 4
Mike Bryant Avatar answered Oct 22 '22 11:10

Mike Bryant