title: How to call Java method in Javascript
body: <p>I am creating web page using JSF 2.0.</p>
  
  <p>What I want to do is, call one method <code>setLastVisitedPage</code> that I have in bean <code>MyBean</code> when everypage loads.</p>
  
  <p>I can do this by simply writing</p>
  
  <pre><code>&lt;h:body onLoad="#{MyBean.setLastVisitedPage()}"
  </code></pre>
  
  <p>however I would have to write in all pages and already there are 30-40 pages created. :(</p>
  
  <p>In page as I have common heading, I have created one file called <code>commonPage.xhtml</code>. I was thinking to include that file here.</p>
  
  <p>So I added below in <code>commonPage.xhtml</code> so that whenever any page is loaded, <code>myScript.js</code> gets called and I get alert as "i m here".</p>
  
  <pre><code>&lt;h:outputScript name="js/myScript.js" target="head" /&gt;
  </code></pre>
  
  <p>and <code>myScript.js</code> has </p>
  
  <pre><code>onload = alert("i m here");
  </code></pre>
  
  <p>Whenever I open any new page, I am getting alert as "i m here".</p>
  
  <p><em><strong>Now my question is how can I add <code>#{MyBean.setLastVisitedPage()}</code> in javascript so that <code>setLastVisitedPage()</code> get called for every page?</em></strong></p>
  
  <p>Is there any alternative way? May be I am thinking in wrong way.</p>