views:

203

answers:

4

How to close a tab or window by clicking on a button in JQuery? I tried window.close() thinking that it would close that window,but it doesn't seem to work. Someone help me.

<?php echo $javascript->link('jquery');?>
<script type="java/javascript">
 $(document).ready(function(){
   $(".close").click(function(){ 
     window.close();
   });
 });
</script>
<div class="close">
   <?php echo $form->button('Close Window');?>
</div>

EDIT

Sorry,The code is correct. The answer didn't come due to a very silly mistake of mine. I had typed script type as java/javascript instead of text/javascript.Now I have changed it and this works fine.

A: 

If the browser window is not opened by the Javascript, it can not be closed by the script.

jatanp
Well. It can, it's just that most browsers will ask the user first.
peirix
A: 

You put the click event on the div, not on the button.

Ikke
+5  A: 

As seth comments, your problem is the handler not being attached because the browser won't execute java/javascript code as it doesn't know what to do with it (since it is nothing). Change it to text/javascript and you should be fine.

Pim Jager
yeh.. you are right..It was a very silly mistake of mine..Sorry to bother everyone..
Angeline Aarthi
+3  A: 

There is actually a function codeBlock in JavascriptHelper that inserts a well-formed JavaScript script tag for you, including the CDATA section delimiters. Used together with PHP's heredoc I think it's a quite neat solution. At least it spares you from tiresome mistakes like these. :)

joelpet
thanks.. i'll look into it.. :)
Angeline Aarthi