views:

66

answers:

3

Is there any possibility to make a link or a button to close the current page? Like I have this:

<div id="arrows">
        <a href="It works"><div id="arrow_left"></div></a>
        <a href="It works too"><div id="arrow_right"></div></a>
        <a href="??? =("><div id="arrow_close"></div></a>   
</div>

And this if it's necessary this:

#arrow_close{
height: 7px;display: inline;float: left;width: 7px;
background: url(../i/close-20.png) no-repeat;
}

And I want to close the page with a button "close".

+3  A: 
<a href="javascript:window.close();">

(some browsers won't allow this if the window wasn't opened by a script, depending on the security features enabled)

Michael Mrozek
doesn't works in Mozilla and IE is asking before closing =\
TRAVA
@TRAVA: live with it, or forget it.
Mef
@Mef: that prompting can be bypassed
Mahesh Velaga
+2  A: 

You can do this inline pretty easily... on a div, you would do

onClick="javascript:window.close();"

But on an <a> tag, you can also do

href="javascript:window.close();"
RussellUresti
How should i use it?onClick="javascript:window.close();"
TRAVA
Using the setup you provided, you can do this one of 3 ways...One.<a href="javascript:window.close();"><div id="arrow_close"></div><a> Two.<a href="#" onClick="javascript:window.close();"><div id="arrow_close"></div></a> Three.<a href="#"><div onClick="javascript:window.close();" id="arrow_close"></div></a> It all depends on if you want the event to be triggered on the anchor or the div.
RussellUresti
Nothing is works in Mozilla.
TRAVA
@TRAVA If you check the javascript console you'll probably see something like "Warning: Scripts may not close windows that were not opened by script." -- that's the security thing I was talking about in [my answer](http://stackoverflow.com/questions/2660418/close-button-on-the-page/2660420#2660420)
Michael Mrozek
+2  A: 

You can use

window.close() or self.close() to close the present window.

To prevent the browser from prompting the warning, you need to set the opener property and then call the close.

<a href="JavaScript:CloseWindow();"><div id="arrow_close"></div></a>
<script type="text/javascript">
var CloseWindow = function() {
  window.top.opener = null;
  window.close();
}
</script>
Mahesh Velaga
Oh, i think it will works. But i'm nooby in js.So it will be like: <script src="final_files/jquery.js" type="text/javascript"> </script> <script type="text/javascript"> $(function(){ ?window.opener = "something"; MyNewDIvWithBUtton.onClick="javascript:window.close();" }); </script>
TRAVA
@trava: updated the post with more code
Mahesh Velaga
Great! Thanks a lot. But it doesnt works in Mozilla =\Is it possible to make it work in Mozilla?
TRAVA
what all browsers did u try? and what is it doing for mozilla?
Mahesh Velaga
try changing window.opener = "something" to window.top.opener = null, updated the post, let me know if this worked
Mahesh Velaga