PDA

View Full Version : Close Browser Script



k12onos
08-11-2006, 10:18 AM
hi guys. i just wanted to ask you of some script that Close the browser when someone click a link. i didnt see any scripts like this in www.dynamicdrive.com scripts.

example :

when someone click a link named 'exit' in a webpage. the browser automatically closed.


can you give me the script ? i have been looking for a while now. but i cant find it.

neilkw
08-11-2006, 10:36 AM
this should do the job.

javascript:self.close()

k12onos
08-11-2006, 10:38 AM
well, should i put it in <script> tag. or where should i put it ?

i want when an image saying Exit is clicked the browser closed without any warning

neilkw
08-11-2006, 10:39 AM
<a href="#" onClick="MM_callJS('javascript:self.close()')">close browser </a>

k12onos
08-11-2006, 10:40 AM
does it work with <img src=#> tag ?

neilkw
08-11-2006, 10:42 AM
<a href="#" onClick="MM_callJS('javascript:self.close()')"><img src="your_image_here.gif" alt="image" width="10" height="10" /></a>

jscheuer1
08-11-2006, 10:42 AM
i want when an image saying Exit is clicked the browser closed without any warning

No warning, huh? I'd say that the image with the word Exit on it would be a dead giveaway.

k12onos
08-11-2006, 10:45 AM
what do you mean it would be a dead giveaway ?

you mean its useless because nobody would click it ?

oh dont worry i know somebody will click it :D

<a href="#" onClick="MM_callJS('javascript:self.close()')"><img src="your_image_here.gif" alt="image" width="10" height="10" /></a>

does this give warnings when we click the image ?
aanyway testing it now

neilkw
08-11-2006, 10:46 AM
The text or image you use to close the window would act as the warning as I assume you'd flag the resultant outcome of clicking the link with text stating " close this window" or something similar.

k12onos
08-11-2006, 10:50 AM
<a href="#" onClick="MM_callJS('javascript:self.close()')"><img src="your_image_here.gif" alt="image" width="10" height="10" /></a>


this one didnt work on either IE or firefox.

neilkw
08-11-2006, 10:53 AM
Did you add your own image?

k12onos
08-11-2006, 10:57 AM
<a href="#" onClick="MM_callJS('javascript:self.close()')">close browser </a>

i tried this one too. without image. its not working too.

jscheuer1
08-11-2006, 11:06 AM
Well the truth of the matter is that any window not opened with javascript cannot be closed with javascript except, some browsers can be fooled sometimes into thinking that a window was opened with javascript when it wasn't.

This code with the MM stuff requires a supporting script on the page.

Something straightforward like this will work if the window was opened using javascript and sometimes even if it wasn't:


<a href="javascript:void(0);" onclick="window.opener=self;self.close();"><img src="exit.gif" border="0"></a>

k12onos
08-11-2006, 11:10 AM
wow, Thanks ! it works perfectly.

thank you very much again Jscheuer

mwinter
08-11-2006, 09:30 PM
<a href="#" onClick="MM_callJS('javascript:self.close()')">

What's with this MM_callJS rubbish? A Dreamweaver thing? *shudder*

The anchor is unnecessary anyway:



<img alt="Replacement text" src="/path/to/file" onclick="window.close();">

The change in cursor can be achieved using CSS, if really necessary.



<img ... />

Pseudo-XHTML is bad, mkay. :)




<a href="javascript:void(0);" ...

Arrgh! See my posts in the thread, Gmail feature (http://www.dynamicdrive.com/forums/showthread.php?p=47092#post47092).

Mike

Twey
08-11-2006, 09:42 PM
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}

function MM_callJS(jsStr) { //v2.0
return eval(jsStr)
}I will never understand the logic behind DreamWeaver. I mean, the wrapper functions don't even have shorter names.

mwinter
08-11-2006, 09:46 PM
I will never understand the logic behind DreamWeaver.

I think it's fair to say that the stock Dreamweaver scripts are pants (other adjectives do come to mind, by the way :)) and shouldn't be used.



I mean, the wrapper functions don't even have shorter names.

Even then, I don't think it's worthwhile to wrap a simple function call in another function. It only adds run-time overhead.

Mike

k12onos
08-12-2006, 12:43 AM
What's with this MM_callJS rubbish? A Dreamweaver thing? *shudder*

The anchor is unnecessary anyway:



<img alt="Replacement text" src="/path/to/file" onclick="window.close();">

The change in cursor can be achieved using CSS, if really necessary.


Pseudo-XHTML is bad, mkay. :)



Arrgh! See my posts in the thread, Gmail feature (http://www.dynamicdrive.com/forums/showthread.php?p=47092#post47092).

Mike
tried this one but it gives a warning when we try to close the page. i want it that it dont give warning.

<a href="javascript:void(0);" onclick="window.opener=self;self.close();"><img src="exit.gif" border="0"></a>

this one works. but what browser is it compatible to ?

Twey
08-12-2006, 12:48 AM
<img alt="Replacement text" src="exit.gif" style="border:0 none;" onclick="window.opener = self; self.close();">

k12onos
08-12-2006, 01:04 AM
can you specify the browsers compability for this one ? and does it give warning?

Twey
08-12-2006, 01:23 AM
can you specify the browsers compability for this one ?All browsers with Javascript, I should think.
and does it give warning?Not if John's didn't.

jscheuer1
08-12-2006, 05:36 AM
No warning but no work in some browsers, most notably FF. I played around with this idea a bit more the other night and came up with this monstrosity that will fool probably almost any javascript enabled browser into closing the window or tab displaying this page (restricted javascript doesn't count):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Self Closing Window/Tab - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript">
if (location.search.indexOf('close')>-1)
self.close();
document.write('<a href="javascript:void(0);" onclick="window.opener=self;self.close();window.open(location.href+\'?var1=close\',\'_top\');">Exit</a>')
</script>
<noscript>Close Window to Exit</noscript>
</body>
</html>

Honestly though, about javascript:void(0); I've never seen a problem with it in any version of IE. And, in the above example, non-scripting browsers aren't even an issue.

k12onos
08-12-2006, 06:47 AM
from all of this i would say that i prefer this one :

<a href="javascript:void(0);" onclick="window.opener=self;self.close();"><img src="exit.gif" border="0"></a>


the browser close it self without any warning. and you said it would compatible with any java script enabled browser. and all browser now is java script enabled. except if the computer is so old that it has a very old version of a browser.



jscheuer, i think that code is a bit complicated, especially with someone like me :P

jscheuer1
08-12-2006, 08:19 AM
from all of this i would say that i prefer this one :

<a href="javascript:void(0);" onclick="window.opener=self;self.close();"><img src="exit.gif" border="0"></a>


the browser close it self without any warning. and you said it would compatible with any java script enabled browser. and all browser now is java script enabled. except if the computer is so old that it has a very old version of a browser.



jscheuer, i think that code is a bit complicated, especially with someone like me :P

Its not so complicated. Also, I think you misunderstand, the code you like will not work in FF, the second most widely used browser in the world. And any browser can be non javascript enabled. It is a setting in the configuration, something that you can set, like the homepage, default mail program, default fonts, etc. The updated version won't work in javascript-less browsers but, will tell folks to close the window to exit or whatever you like. Here is a similar but even better version, I just thought up:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Self Closing Window/Tab - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript">
if (window.name=='bob')
self.close();
else
document.write('<a href="javascript:void(0);" onclick="window.opener=self;self.close();window.name=\'bob\';window.open(location.href,\'bob\');">Exit</a>');
</script>
<noscript>Close Window to Exit</noscript>
</body>
</html>

mwinter
08-12-2006, 08:49 AM
No warning but no work in some browsers, most notably FF.

That's because Firefox doesn't suffer from the same bug as MSIE. What I posted will close a pop-up, though.

One should expect the exploit, wherein assigning a value to the opener property allows a window to be closed without asking for confirmation, to be closed at some point in the future. The notice is given for a reason and it's only because a simplistic check is used to determine whether a window is a pop-up that makes the exploit viable in the first place.



Honestly though, about javascript:void(0); I've never seen a problem with it in any version of IE.

It exists, I can assure you. A simple demonstration (http://mwinter.webhop.info/dd/john-scheuer/pseudo-scheme.html) should suffice.

Mike

jscheuer1
08-12-2006, 09:43 AM
It exists, I can assure you. A simple demonstration (http://mwinter.webhop.info/dd/john-scheuer/pseudo-scheme.html) should suffice.


<a href="javascript:void(0);" onclick="return false;">Test</a></div>

Which is what you have to do anyway with '#'. Does this really mess up anything else besides animated .gif's? IE is known to have issues with those anyway. Who knows, this could be a feature, not a bug. I tried this:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>Pseudo-scheme Test</title>
<script type="text/javascript">
setInterval("alert('here')", 5000);
</script>
</head>

<body>
<h1>Pseudo-scheme demonstration</h1>
<div><a href="javascript:void(0);">Test</a></div>
</body>
</html>


It did not mess up the interval. But, I do see your point after rereading the linked thread you mentioned. However, in my example, in IE, that is exactly what is happening, the page is unloading.

mwinter
08-12-2006, 10:03 AM
<a href="javascript:void(0);" onclick="return false;">Test</a></div>


Which is what you have to do anyway with '#'.

The latter isn't nice, either, and the above still isn't the end of issues with using the javascript pseudo-scheme.

[ed: removed comment; you edited]



IE is known to have issues with those anyway. Who knows, this could be a feature, not a bug.

When did I say it was a bug?

Mike

jscheuer1
08-12-2006, 10:28 AM
When did I say it was a bug?

Er, I guess you didn't. I've been trying to work that phrase into my posts more lately. :) Anyways, thanks for pointing this out to me and all of us who are paying attention. I think I was just starting to get a bit careless in my use of it (starting to treat it the same as return false). So, sooner or later I would have run into trouble with it and probably not known why. I'll probably continue to use it as, it is a habit, but also continue to use return false with it when the page isn't unloading.