sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Webdialogs and Javascript void

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 6 Posters 8.8k Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P Offline
      Pout
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • M Offline
        MartinRinehart
        last edited by

        What does void(0) do?

        Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

        1 Reply Last reply Reply Quote 0
        • chrisglasierC Offline
          chrisglasier
          last edited by

          *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.*

          With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            The result of this is the same as returning false in the event to prevent the browser from further processing the event?

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • chrisglasierC Offline
              chrisglasier
              last edited by

              When I saw Mr J S Lint didn't care for it I investigated no further.

              With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

              1 Reply Last reply Reply Quote 0
              • P Offline
                Pout
                last edited by

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

                1 Reply Last reply Reply Quote 0
                • M Offline
                  MartinRinehart
                  last edited by

                  @pout said:

                  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?

                  Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    MartinRinehart
                    last edited by

                    @dan rathbun said:

                    @martinrinehart said:

                    Does void differ from return null?

                    YES. There's a gotcha (because of backward compatibility.)
                    %(#BF00BF)[(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&#058;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"?)

                    Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      @martinrinehart said:

                      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:
                      %(#804000)[<a href=']%(#8000BF)[javascript&#058;return null]%(#804000)[' ondblclick=']%(#8000BF)[...]%(#804000)['>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 said:

                      And do you agree with Crockford? **> @unknownuser said:

                      **.
                      **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.] **> @unknownuser said:

                      **.
                      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 exampleis 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 said:

                      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* )

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • L Offline
                        leneborma
                        last edited by

                        @martinrinehart said:

                        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.

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by

                          @martinrinehart said:

                          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:
                            %(#BF00BF)[if(typeof(var)=="undefined") //do something]
                            You check null without quotes:
                            %(#BF00BF)[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.)
                            %(#BF00BF)[(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:
                            %(#BF00BF)[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:
                            %(#BF00BF)[function void( *expression* ) {] %(#F0F0F0)[___]%(#BF00BF)[eval( *expression* );] %(#F0F0F0)[___]%(#BF00BF)[return( undefined ); }]
                            ___

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement