开发者

close window from javascript

开发者 https://www.devze.com 2022-12-21 13:39 出处:网络
How to close the current window from javascript without any events like onclic开发者_运维百科k or something else..

How to close the current window from javascript without any events like onclic开发者_运维百科k or something else..

   <script>
   window.close();
   </script>

The above doesnt seems top work..

Edit: i am opening a child window from a parent window and after some operation i want to close the child window and window.close(); doesnt seem to work

Thanks..


Well, when do you want it to trigger? It will need some kind of event to trigger it, either on the document ready (if you want to close it immediately after opening it), or on some other event (a timer, a click, whatever).

If you explain what you want to happen, it's probably easy to solve.

EDIT: Sorry, just checked it in FireBug, and Oded is correct. FireBug says scripts are not allowed to close windows that where not opened by script. But if it where, it should work...

EDIT2: I used this code to open a new window:

$(function(){
    window.open('/test2.html','test2','width=300,height=200');
});

And then the following in the newly opened window:

<!DOCTYPE html>
<html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    </head>
    <body>
    <script type="text/javascript">
    $(function(){
        window.close();
    });
    </script>
    test
    </body>
</html>

This worked for me.

EDIT3: Ripping out the jQuery and running the script in the body with no document ready checking also worked, but that might fail if you want to do anything other than closing the window. Any kind of document ready checking should work though. I just used jQuery because it's simple... :)


I've noticed that window.close() has to be within a function, even an anonymous one -- just like that it wouldn't work. Try putting it in a function and calling it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号