PDA

View Full Version : define "void" in <a href="javascript:void closeme()">


skeatt
11-26-2002, 03:47 PM
"VOID" I've seen this in the script below and know that it works, but I want to know why it needs this? and whats it's purpose in this argument?

Why do we use it in some places and not others, like below

<a href="javascript:loadWin('test2.html')"> 3) open window</a><br><br>
<a href="javascript:void closeme()"> close window<br>

In one post by glenngv:.....
use void before the window.open method so that it will not return the window object.

Window object? please explain, is this different to the above.

ahosang
11-26-2002, 04:52 PM
when a new window is created, the function will return the window object(the new one) if nothing else. And since this code is called from href attribute, the browser will attempt to go to that returned text, which is the object's toString result:
object [object]. So a blank screen usually appears with just that text on it(your original page disappears). Using void operator means that no return is passed to the href. In fact that javascript pseudo-URL technique is obsolete and is best to use:
<a href="#" onclick="closeMe();return false">

skeatt
11-27-2002, 03:36 AM
Thanks ahosang,:thumbsup:

I'll try the <a href="#" onclick="closeMe();return false"> method and see if my coding still works with it.

realisis
11-27-2002, 07:06 AM
I agree with ahosang's recommendation regarding using

<a href="#" onclick="closeMe();return false">

besides which, if you use the void() method, and your viewer happens to right-click on the link, and then chooses "Open link in a new window" - a nasty error results. Using onClick() instead at least avoids the error.

glenngv
11-27-2002, 07:56 AM
I agree with ahosang's too but not on what you said...
it's using javascript protocol in the href that may produce error (not on using void) if the user chooses to open the link in a new window.

realisis
11-27-2002, 08:09 AM
quite right, Glenn.

I'd expressed the same thing properly on a previous thread, but had a mental lapse this time I guess - thanks for pointing it out.

skeatt
11-27-2002, 08:39 AM
I tried it....once I got my parenthesis correct it worked just as pseudo-URL.
When do you use the "#" or make it ;return true"?

The following is correct isn't it when sending a page?

<a href="#" onclick="loadWin('test1.html');return false"> 1) open window</a><br>
<a href="#" onclick="loadWin('test2.html');return false"> 2) open second window</a><br>
<a href="#" onclick="closePopUp();return false"> close using "#" </a><br>

glenngv
11-27-2002, 08:56 AM
you always return false to cancel the href which doesn't necessarily be "#".

the links could be like these:


<a href="test1.html" target="_blank" onclick="loadWin('test1.html');return false;"> 1) open
window</a><br>
<a href="test2.html" target="_blank" onclick="loadWin('test2.html');return false;"> 2) open second window</a><br>
<a href="//" onclick="closePopUp();return false"> close using "//" </a><br>


This will degrade well with javascript-disabled browser (except of course for the "close" link). It will still open the page in a new window but without features.

skeatt
11-27-2002, 09:42 AM
:D glenngv: At first I had a :confused: look, but then it clicked, what I did wasn't incorrect but the way you showed was a fail safe method for people who don't like Javascript Enabled.

I guess the target="_blank" is part of this failsafe scripting bit

One last question...why the "//" in the...
<a href="//" onclick="closePopUp();return false"> close using "//" </a>

glenngv
11-27-2002, 10:02 AM
nothing :D
just showed you that it doesn't necessarily be "#"
though that's ("//") not safe with javascript-disabled browser.
so using "#" is the safest if you are not calling window.open and just calling a function, closePopUp() in your case.