admin管理员组

文章数量:1319953

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 e 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.

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 e 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.

Share Improve this question edited Aug 26, 2009 at 11:16 Pim Jager 32.1k17 gold badges74 silver badges99 bronze badges asked Aug 19, 2009 at 6:45 AngelineAngeline 2,37922 gold badges69 silver badges107 bronze badges 3
  • 5 type="java/javascript"? That's should be "text/javascript" – seth Commented Aug 19, 2009 at 6:47
  • sorry that was careless mistake.. – Angeline Commented Aug 19, 2009 at 8:23
  • yeh.. you are right.. It was a very silly mistake of mine.. Sorry to bother everyone.. – Angeline Commented Aug 19, 2009 at 8:27
Add a ment  | 

5 Answers 5

Reset to default 6

As seth ments, 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.

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. :)

<a href="javascript:window.open('','_self').close();">close</a>

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

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

本文标签: javascriptClose a window on button clickStack Overflow