Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What's a practical use for the void operator?

    MSDN says:
    It is most useful in situations where you want an expression evaluated but do not want the results visible to the remainder of the script.

    ..and I say: Huh?
    *this message will self destruct in n-seconds*

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    say, for instance, you are invoking the open() method from an anchor. The void() operator cancels the anchors normal behavior--to open a new [standard] browser window--which you want to replace with a custom js window.

    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #3
    eak
    eak is offline
    Regular Coder eak's Avatar
    Join Date
    Jun 2002
    Location
    Nashville, TN
    Posts
    354
    Thanks
    0
    Thanked 26 Times in 26 Posts
    msdn is exactly right on this. for example when i want an html link to do nothing i do this:
    Code:
    <a href="javascript:void()">dummy link</a>
    that is a basic example of void(). you can also do this
    Code:
    function noReturn(){return "this is text";}
    document.write( noReturn() );
    document.write( void( noReturn() ) );
    that should write out "this is text" only one time.
    eak | "Doing a good deed is like wetting your pants; every one can see the results, but only you can feel the warmth."

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but how is it practical to go: "void function call()"? It seems like that whole line has no purpose.

    I was hoping void might be useful with custom objects, or...? I have no clue.
    *this message will self destruct in n-seconds*

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, and I forgot to mention that I'd seen the javascript:void() thing before... but thought to myself: the operator couldn't possibly have been put in JS only to break hrefs.

    a n y w a y...
    *this message will self destruct in n-seconds*

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    To see the utility of void() try this

    <a href="javascript:window.open('popup.html')">link</a>

    You will have the surprise to see that after opening the popup, the opener will try also to do a HTML action. From the point of HTML view, the expresion between quotes might be an new window, a new object (IE says [object], Mozilla says even clearer [object Window]) so the browsers will, let's say, "create" a new page emulating it.

    To avoid this, void() is a much useful method, so the correct syntax is:
    <a href="javascript:void(window.open('popup.html'))">link</a>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can we get beyond the href example, or is that all we know?

    That's ok... it's all I know too.
    *this message will self destruct in n-seconds*

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    the less known thing is that, in fact void is an operator which can (and it is mostly used) as a function

    void statement
    or
    void(statement)

    (same as typeof or eval which are in fact operators)

    No, I don't know any other noticeable use for void() except the href blocking.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I now remember having seen some VBS examples that used void in some way.
    Maybe I can convert those to JS for all the wrong reasons...
    *this message will self destruct in n-seconds*

  • #10
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I take that back... there is no 'void' in VBS!
    *this message will self destruct in n-seconds*

  • #11
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FWIW: void can also be used to remove the value of a variable...

    If you have a variable var test = 'test'
    You would use test = void to remove the value without having this empty variable return null or undefined...

    .....Willy

  • #12
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can't do that; it generates an error, because void is an operator...

    var foo = 1;
    alert(typeof(void foo));


    returns undefined, but doesn't change foo's type.
    *this message will self destruct in n-seconds*

  • #13
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I said change its value not type....

  • #14
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So are you maintaining that an operator can be assigned to a variable?
    I appreciate the suggestion, but that just doesn't happen.
    *this message will self destruct in n-seconds*

  • #15
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Let's bring a little light on what void does in JavaScript:
    - First of all, void is older than the undefined keyword. From the start, using void was the only way you could yield undefined. Later, when functions became first class objects in the language (we're talking about pre-ECMA time here), they also by default returned undefined. (Before that they had returned without a value, which is not quite the same.)
    - You might have problems believing it, but in fact void was introduced with a single purpose in mind: cancelling the following of links. Again, this was before the DOM0 event model was set in stone and more consistent methods for doing that was created.
    - void is not a function, though some implementations may treat it like that. It's a unary operator, which you may give a unary expression as argument. If this unary expression happens to be a parentesised expression, so be it, but it's still an operator and not a function. (This is of little consequense for void since it acts like a black hole, but consider the sibling operator typeof instead, which acts different from how it would have acted were it a function.)

    Of what use is the void keyword?
    In the beginning, before the DOM0 event model, it could be useful for yielding undefined to cancel the following of links on webpages. Now, we don't need it for that any longer, we return false in the onclick event handler instead.
    Also, if we wanted to explicitly compare a value to undefined we could use void since we had no keyword yielding undefined back then.
    Today, the sole place it's sensible to use void is within bookmarklets, since we do not have the event model to replace it's functionality in a pure pseudo-URI.



    Edit: Willy, have you tried that? Operators are not expressions, and they are not values. You cannot assign an operator to an identifier, it will result in a syntax error.
    Last edited by liorean; 08-25-2004 at 06:41 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •