Webdialogs and Javascript void

Webdialogs and Javascript void

Postby Pout » Mon Jan 18, 2010 12:45 pm

I have spend a serious amount of time figuring out why some of my buttons in javascript did not work.
It was due to the void(0) parameter which can be set.

Example:
<a href="JavaScript:void(0);" ondblclick="alert('Well done!')">Double Click Me!</a>


Hope this helps anyone.

Pout
0

Pout 
 

Re: Webdialogs and Javascript void

Postby MartinRinehart » Tue Jan 19, 2010 11:01 pm

What does void(0) do?
0
Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

MartinRinehart 
 

Re: Webdialogs and Javascript void

Postby chrisglasier » Wed Jan 20, 2010 1:18 am

void

In most C-like languages, void is a type. In JavaScript, void is a prefix operator that always returns undefined. JSLint does not expect to see void because it is confusing and not very useful.
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: Webdialogs and Javascript void

Postby thomthom » Wed Jan 20, 2010 9:11 am

The result of this is the same as returning false in the event to prevent the browser from further processing the event?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: Webdialogs and Javascript void

Postby chrisglasier » Wed Jan 20, 2010 9:25 am

When I saw Mr J S Lint didn't care for it I investigated no further.
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: Webdialogs and Javascript void

Postby Pout » Wed Jan 20, 2010 12:28 pm

True, but some sites refer to it as usable in JS (which it is in fact)
Example:
http://www.tizag.com/javascriptT/javascriptvoid.php
0

Pout 
 

Re: Webdialogs and Javascript void

Postby MartinRinehart » Wed Jan 20, 2010 4:16 pm

Pout wrote:True, but some sites refer to it as usable in JS (which it is in fact)
Example:
http://www.tizag.com/javascriptT/javascriptvoid.php


Does void differ from return null?
0
Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

MartinRinehart 
 

Re: Javascript: void(0) vs return(null)

Postby Dan Rathbun » Thu Jan 21, 2010 6:35 am

MartinRinehart wrote:Does void differ from return null?

YES. (in the following, violet denotes javascript.)

(1) return type: undefined and null are separate return types.
___http://msdn.microsoft.com/en-us/library/7wkd9z69(VS.85).aspx
  • void( whatever ) always returns undefined
  • return( null ) obviously will return null (in this case.)
A variable can have a null value, but not an undefined value, because undefined refers to the object, not it's value. You check undefined using quotes:
if(typeof(var)=="undefined") //do something
You check null without quotes:
if(var==null) //do something
The difference is subtle. Basically, if a symbol references a variable that has not been declared, or has been declared but never had a value assigned, then "undefined" is returned. If a symbol references a variable that has had any value (including null,) assigned to it, it will not return "undefined".
But be careful! There's a gotcha (because of backward compatibility.)
(object.prop==null) will return true if the property has the value null or if the property does not exist! You can use in to be more specific:
if ("prop" in object) will return true if the property does exist.

(2) Both are evaluation functions:
  • return( expression ) [called a Statement] Always returns the result (of whatever type,) that expression evaluates to.
    http://msdn.microsoft.com/en-us/library/22a685h9(VS.85).aspx
  • void( expression ) [called an Operator] Evaluates expression then always returns "undefined" (regardless of the result.) The expression is required or an argument error would be raised. The expression 0 is traditional, and comes from C language (where null and zero were interchangable; but in javascript, 0 is a number type, null is it's own type.)
    http://msdn.microsoft.com/en-us/library/e17c7cbe(VS.85).aspx
    The internal code might be represented as:
    function void( expression ) {
    ___eval( expression );
    ___return( undefined );
    }
___
0
Last edited by Dan Rathbun on Sat Jun 06, 2020 3:22 pm, edited 1 time in total.
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Javascript: void(0) vs return(null)

    Postby MartinRinehart » Thu Jan 21, 2010 3:10 pm

    Dan Rathbun wrote:
    MartinRinehart wrote:Does void differ from return null?

    YES. There's a gotcha (because of backward compatibility.)
    (object.prop==null) will return true if the poperty has the value null or if the property does not exist!


    Dan, I enjoyed your very deep response. Thank you!

    But my question was really about the original proposed use. I get the intended result with <a href='javascript:return null' ondblclick='...'>doubleclick-only link!</a>, without offending Doug Crockford.

    And do you agree with Crockford? "In most C-like languages, void is a type. In JavaScript, void is a prefix operator that always returns undefined. JSLint does not expect to see void because it is confusing and not very useful." (Why "prefix operator"?)
    0
    Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

    MartinRinehart 
     

    Re: Javascript: void(0) vs return(null)

    Postby Dan Rathbun » Thu Mar 04, 2010 2:36 am

    MartinRinehart wrote:Dan, I enjoyed your very deep response. Thank you!
    But my question was really about the original proposed use. I get the intended result with:
    <a href='javascript:return null' ondblclick='...'>doubleclick-only link!</a>
    without offending Doug Crockford.
    (1) Not in my opinion,... you are 'misusing' return in the same way that offends Doug regarding the misuse of void.
    And that is, misusing the function (or operator,) by forcing the processing of a meaningless literal expression such as 0, (ie: void(0);) when it is preferred for clarity, and faster for processing, to simply use return; (as return with no expression returns undefined.)
    Doug also seems to imply that the choice of the word 'void' for the Js "return_undefined_after_eval" function name, is confusing because of it's accepted use in other languages. I give him that. The inventer of Js could have named it the noreturn operator.
    (2) As mentioned above, there can be compatibility problems with the use of null. (And, once again, why force the machine to process the null expression if it is not necessary?)
    MartinRinehart wrote:And do you agree with Crockford?
    Doug Crockford wrote:.
    In most C-like languages, void is a type. In JavaScript, void is a prefix operator that always returns undefined."
    [Dan inserts: 2 statements of fact - agreement not necessary.]
    Doug Crockford (continuing,) wrote:.
    JSLint does not expect to see void because it is confusing and not very useful.
    Two opinions:
    (1) "confusing" - The void operator, is only confusing when it's misused, (as stated above.)
    (2) "usefulness" - The void operator was impletemented for a real purpose. When the expression to be evaluated, is a valid expression (processing work needs to be done to evaluate it, and that evaluation is necessary;) but the results need to be prevented from returning to the caller.
    The perfect example is the problem Todd was having with Js callbacks to a Sketchup Ruby. In that situation, the proper use of void is a valid answer to the problem.
    See SCF topic: Anyone seen this web dialog bug?

    MartinRinehart wrote:Why "prefix operator"?
    A prefix operator is one that is stated before an expression (ie: it operates on the expression that follows itself.)
    In Ruby, we have some "double-duty" reserved words that can also be used as prefix operators. Examples are if and unless. (Although the Ruby manual calls them 'modifiers' when they are used in this way.)
    ( expression that does something ) if ( boolean expression )
    or
    ( expression that does something ) unless ( boolean expression )
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: Webdialogs and Javascript void

      Postby leneborma » Mon Feb 10, 2020 11:21 am

      MartinRinehart wrote:What does void(0) do?


      The void operator evaluates the given expression and then returns undefined. If you have pass 0 as the unary expression operand to the void operator, JavaScript coerces 0 to "false" and returns, but void doesn't care and simply returns undefined, which means "do nothing" . Put them together and you have composed a way to programmatically "do nothing" when a link is clicked. JavaScript Void(0) is often used when, inserting an expression into a web page may produce an unwanted side-effect.
      0

      leneborma 
       

      SketchUcation One-Liner Adverts

      by Ad Machine » 5 minutes ago



      Ad Machine 
      Robot
       



       

      Return to Developers' Forum

      Who is online

      Users browsing this forum: No registered users and 7 guests

      Visit our sponsors: