title: Calling a C# function through Javascript onClick event in WebView in Xamarin.Forms
body: <p>I have a post type <code>WebView</code> which I managed to bind with the service response as string but I have some links like related posts which have their ids. On clicking  those links I want the user to go to that article. I have tried many solutions but its look like the <em>JavaScript</em> doesn't calls on click, it calls on load because my complete <em>WebView</em> is treated as string and if I concatenate it, it definitely doesn't remains a script.</p>
  
  <p>Here is my complete WebView code and the screenshot attached is the link which is made in WebView.</p>
  
  <p>I managed to make it work by concatenating the whole response in string.
  Below is the code  which helped me achieve this</p>
  
  <pre><code>String getHtml()
  {
      var bodyStyle = "&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;style&gt;body{height: 100%;}p{text-align:left;color:#191919;}filter{background-color:#191919;}a:link"
          + "{color:#2588B0; background-color:transparent}a:visited {color:#0099CC; background-color:transparent}"
          + "a:hover{color:#0099CC; background-color:transparent}a:ative{color:#0099CC; background-color:transparent}span{background-color: yellow;color: black}customRight{float: right}&lt;/style&gt;&lt;/head&gt;&lt;body&gt;";}
  
      var refs = bodyStyle;
  
      refs = refs + "&lt;center&gt;&lt;h4&gt;" + response.Title + "&lt;h4&gt;&lt;/center&gt;";
  
      if (response.Pc.Count &gt; 0)
      {
          refs = refs + "&lt;center&gt;&lt;u&gt;" + Pc +
              "&lt;/a&gt;&lt;/u&gt;&lt;/center&gt;&lt;br&gt;";
      }
  
      if (string.IsNullOrWhiteSpace(response.Cn))
      {
          refs = refs + "&lt;center&gt;" + response.Cn + "&lt;/center&gt;&lt;br&gt;";
  
      }
  
      if (response.Judges.Count() &gt; 0)
      {
          refs = refs + "&lt;center&gt; &lt;strong&gt;Coram:&lt;/strong&gt; " + JudgesN + "&lt;/center&gt;&lt;br&gt;";
      }
  
      if (string.IsNullOrWhiteSpace(response.JGD.ToString()))
      {
          refs = refs + "&lt;center&gt; &lt;strong&gt;Decided On:&lt;/strong&gt; " + response.JGD + "&lt;/center&gt;&lt;br&gt;";
      }
  
      if(string.IsNullOrWhiteSpace(response.AppealType) )
      {
          refs = refs + "&lt;center&gt; &lt;strong&gt;Appeal Type: &lt;/strong&gt;" + response.AppealType + "&lt;/center&gt;&lt;br&gt;";
      }
  
      if (response.Appellants != null)
      {
          refs = refs + "&lt;left&gt;&lt;b&gt;" + apeal + "&lt;/b&gt;&lt;/left&gt;";
          refs = refs + "&lt;customRight&gt;apeal&lt;/customRight&gt;&lt;br&gt;";
  
      }
      refs = refs + "&lt;center&gt;VERSUS&lt;/center&gt;";
  
      if (response.Respondants != null)
      {
          refs = refs + "&lt;left&gt;&lt;b&gt;" + resp + "&lt;/b&gt;&lt;/left&gt;";
          refs = refs + "&lt;customRight&gt;resps&lt;/customRight&gt;&lt;br&gt;&lt;br&gt;";
      }
  
      if (string.IsNullOrWhiteSpace(response.FinalVr))
      {
          refs = refs + "&lt;center&gt; &lt;strong&gt;Final:&lt;/strong&gt; " + response.FinalVr + "&lt;/center&gt;&lt;br&gt;";
      }
  
      if (string.IsNullOrWhiteSpace(response.note))
      {
          refs = refs + "&lt;p&gt; &lt;strong&gt;Note:&lt;/strong&gt;&lt;br/&gt; " + response.Headnote.ToLowerInvariant() + "&lt;/p&gt;&lt;br&gt;";
      }
  
      if(response.Refs != null)
      {
          refs = refs + "&lt;left&gt;&lt;b&gt;Refered Jdgmts: &lt;/b&gt;&lt;/left&gt;&lt;br&gt;";
          foreach(var obj in response.Refs) 
              {
              if(obj.Jid == null) {
                  refs = refs + "&lt;p style='font-size:13px;'&gt;"
                      + obj.Title
                      +"&lt;/p&gt;";
              }
              else
              {
                  refs = refs + "&lt;p style='font-size:13px;'&gt;" + "&lt;a href=\""
                      + obj.Jid
                           + "\" target=\"_blank\" onClick=\"(function(e){alert('e is here'); "+ loadJt(obj.Jid);+"return false;})(); return false;\"&gt;"
                           + obj.Title
                      + "&lt;/a&gt;"
                      + "&lt;/p&gt;";
              }
  
      jdgmt = jdgmt.Replace("^^^", "&lt;br/&gt;&lt;br/&gt;");
      jdgmt = jdgmt.Replace("&lt;SPARA&gt;", "&lt;p&gt;");
      jdgmt = jdgmt.Replace("&lt;/SPARA&gt;", "&lt;/p&gt;");
  
      refs = refs + jdgmt;
      refs = refs + "&lt;/body&gt;&lt;/html&gt;";
  
      return refs;
  }
  
  data.Source = new HtmlWebViewSource { Html = getHtml(), BaseUrl="http://url.com/", BindingContext = response };
  </code></pre>
  
  <p>Here is my LoadJt Function</p>
  
  <pre><code>string loadJt(string jjid)
  {
      loadJmt(jid);// invokes the constructor and updates my VM too.
      return jid;
  }
  </code></pre>
  
  <p>Below how links are displaying</p>
  
  <p><a href="https://i.stack.imgur.com/CesNM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/CesNM.png" alt="attached screenshot to clear up things more."></a></p>