<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bugs On Steroids &#187; Apache Wicket</title>
	<atom:link href="http://blog.e-svet.si/tag/apache-wicket/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.e-svet.si</link>
	<description>Not Another Programming Blog</description>
	<lastBuildDate>Wed, 26 Feb 2014 14:15:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.6</generator>
		<item>
		<title>Closing the browser window from Wicket</title>
		<link>http://blog.e-svet.si/2013/05/closing-the-browser-window-from-wicket/</link>
		<comments>http://blog.e-svet.si/2013/05/closing-the-browser-window-from-wicket/#comments</comments>
		<pubDate>Wed, 29 May 2013 21:05:32 +0000</pubDate>
		<dc:creator>Luka Murn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Apache Wicket]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.e-svet.si/?p=17</guid>
		<description><![CDATA[On the project I was working on I stumbled upon a problem that when a user makes a certain action, the browser window needs to close. The internet is full of solutions for closing the browser window via JavaScript. This StackOverflow topic offers a great script example how to achieve the closing of the browser ...<a class="post-readmore" href="http://blog.e-svet.si/2013/05/closing-the-browser-window-from-wicket/">read more</a>]]></description>
				<content:encoded><![CDATA[<p>On the project I was working on I stumbled upon a problem that when a user makes a certain action, the browser window needs to close. The internet is full of solutions for closing the browser window via JavaScript. <a href="http://stackoverflow.com/questions/57854/how-can-i-close-a-browser-window-without-receiving-the-do-you-want-to-close-thiadly">This StackOverflow topic</a> offers a great script example how to achieve the closing of the browser using simple JavaScript:</p>
<pre class="brush: java; title: ; notranslate">
function closeWindows() {
    var browserName = navigator.appName;
    var browserVer = parseInt(navigator.appVersion);
    //alert(browserName + &quot; : &quot;+browserVer);

    //document.getElementById(&quot;flashContent&quot;).innerHTML = &quot;&lt;br&gt;&amp;nbsp;&lt;font face='Arial' color='blue' size='2'&gt;&lt;b&gt; You have been logged out of the Game. Please Close Your Browser Window.&lt;/b&gt;&lt;/font&gt;&quot;;

    if(browserName == &quot;Microsoft Internet Explorer&quot;){
        var ie7 = (document.all &amp;&amp; !window.opera &amp;&amp; window.XMLHttpRequest) ? true : false;  
        if (ie7)
        {  
          //This method is required to close a window without any prompt for IE7 &amp; greater versions.
          window.open('','_parent','');
          window.close();
        }
       else
        {
          //This method is required to close a window without any prompt for IE6
          this.focus();
          self.opener = this;
          self.close();
        }
   }else{  
       //For NON-IE Browsers except Firefox which doesnt support Auto Close
       try{
           this.focus();
           self.opener = this;
           self.close();
       }
       catch(e){

       }

       try{
           window.open('','_self','');
           window.close();
       }
       catch(e){

       }
   }
}
</pre>
<p>Sadly, Firefox happens to be the exception here, as you cannot close it from the JavaScript. For other browsers, this script should work. Alright, but how do we hook this JavaScript to Wicket? First, we (obviously) have to include the .js file that contains the above script into our page. In Wicket 1.5, we then actually have multiple options for calling this JavaScript from code. Let&#8217;s say we have the following class:</p>
<pre class="brush: java; title: ; notranslate">
public class CloseWindowPage extends WebPage implements IHeaderContributor {

    private AjaxLink&amp;lt;Void&amp;gt; link1;
    private AjaxLink&amp;lt;Void&amp;gt; link2;

    private boolean closeWindow = false;

    @Override
    public void renderHead(IHeaderResponse response) {
        if (this.closeWindow)
            // This JS is executed after the page is completely loaded
            response.renderOnLoadJavaScript(&quot;closeWindows();&quot;);
    }

    public CloseWindowPage(PageParameters parameters) {
        super(parameters);

        if (!parameters.get(&quot;close&quot;).isEmpty())
            this.closeWindow = true;

        link1 = new AjaxLink&amp;lt;Void&amp;gt;(&quot;link1&quot;) {
            @Override
            public void onClick(AjaxRequestTarget target) {}
        };
        link1.add(new AttributeAppender(&quot;onclick&quot;, &quot;closeWindows();&quot;);
        add(link1);

        link2 = new AjaxLink&amp;lt;Void&amp;gt;(&quot;link2&quot;) {
            @Override
            public void onClick(AjaxRequestTarget target) {
                // Do some stuff...

                // Redirect to this page, set the parameters
                PageParameters params = new PageParameters();
                params.add(&quot;close&quot;, &quot;true&quot;);
                setResponsePage(CloseWindowPage.class, params);
            }
        };
        add(link2);
    }
}
</pre>
<p>I added two links to our sample page. The first option to call this JS function is not really much of a Wicket solution &#8211; basically, you create a HTML link (or a button), and set its <em>onclick</em> attribute to call our JavaScript function, like this:</p>
<pre class="brush: java; title: ; notranslate">&amp;lt;a href=&quot;#&quot; onclick=&quot;closeWindows();&quot; wicket:id=&quot;link1&quot;&amp;gt;Click me&amp;lt;/a&amp;gt;
</pre>
<p>To set the attribute of the element from Wicket, we use the <em>AttributeAppender</em> class, but in essence, it does the same as you would write the event handler in HTML. <em>link1</em> is an example of such behavior. My case, however, was different; when a user clicks a button/link in our application, the Wicket must do some background work, before the browser should close. In this case, the <em>AttributeAppender</em> will not work. We solve this problem by using the <em>renderHead</em> overriden method, and depending on the page parameter, we inject the JavaScript code calling our funcion into the <em>body onload</em> attribute. All we are left to do is to implement a link, override its <em>onClick</em> method, do the work we need and finally redirect to the same page, using the page parameter. This behavior is demonstrated by the <em>link2</em> component. I hope I offered some insight and threw an idea or two out there. Now to wait for Firefox to enable the window closing action&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.e-svet.si/2013/05/closing-the-browser-window-from-wicket/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
