mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 Breaking Bad Habits

Author Message
TNO

  • Total Posts : 2091
  • Scores: 34
  • Reward points : 0
  • Joined: 12/18/2004
  • Location: Earth
  • Status: offline
Breaking Bad Habits Sunday, March 30, 2008 8:31 AM (permalink)
0
This is more aimed to those brand new to programming or brand new to html development, but as always, criticisms and additions are welcomed.

VBScript I think is renowned for its ease of use for programming, and for getting things done quickly while maintaining readability. This plus Internet Explorer's tendency to render almost anything you dump into a webpage makes it that much easier for the inexperienced and experienced to get something done. (It could make utterly no sense in the order of the html tags but IE just might render it "correctly" anyway). But with such ability to mean what you say instead of say what you mean in developing your programs, you set yourself up for a massive failure when your browser/HTA no longer interprets your meaning like it used to:




So if you are building your applications the wrong way from day one without knowing it, then when the browser finally does the right thing (IE8) and not guess your meaning, you are no longer saying what you meant to when you first wrote your program. So how do you fix this? How do you prevent yourself from being one of those people who have to rewrite their program every time a new browser comes out? What if you are shipping your HTA on a CD and have no way of fixing it? Well this is what I hope to help you out with and clarify a bit.

#1 - Proper page formatting:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 	<head>
 		<title>My Foo Bar Application</title>
 
 		<!-- hta tag should go around here //-->
 
 		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 
 		<style type="text/css">
 			#Foo{...}
 		</style>
 
 		<script type="text/vbscript">
 			Option Explicit
 
 			Dim some 'You Dim some, you lose some...
 
 		</script>
 	<head>
 	<body>
 
 		<script type="text/vbscript">
 			'Maybe something more?
 		</script>
 	</body>
 </html>
 


Now to explain why. First off having a doctype is extremely important. This tells the browser what you are actually writing so it doesnt have to guess. (It guesses wrong alot more than you think). Also declaring a doctype will allow you to do more cool things than you could without (Which I wont go over in this initial post. you can read about this on the quirksmode website).

Next thing to notice is that html tags are all lower case now. Those of you who have built web pages long enough may have noticed this change in the trend. (You use to make all html tags uppercase). This is no longer the case, so try and break the habit and keep them lowercase. For now your pages will still work, but to insure future compatibility and to save yourself some effort, try to keep them all lowercase. (Not including doctype, since this is not an html tag, but a browser instruction.)

Indentation. Indenting tags is for readability so you and others don't get lost in your code. You can use either 4 spaces or a single tab. Since there is no written specification on which to use, Its probably best to go off of the "common practice" of using 4 spaces per indent.

If your making an HTML application, stick that hta right below the title tag like Microsoft suggests. There is no common standard for this, and your page wont break otherwise, but at least everyone will know where to look for it.

The content type meta tag. This is a bit more detailed on the explanation why and such so to keep it simple and straight to the point, I SUGGEST you use UTF-8 instead of other encodings so you don't get those weird square blocks on your page when you mess with non-ASCII characters. Plus you're required by the W3C standard to include this meta tag anyway, so might as well do it right. Once again, your page will still work without it, but you may run into some unexpected results down the road if you don't follow the habit.

CSS (the stylesheet <style> tag). Something else that I have seen ignored pretty often is people not using a mimetype. (type="text/css"). You may not know it, but there is more than 1 type of stylesheet language out there so its best not to give the browser the opportunity to mess this up and make a guess at what you write, so include this. Its also important to keep that stylesheet high up in your page, so the browser knows what stuff is supposed to look like before it draws it on your screen. (Remember using the older browsers that would draw a big table on a webpage, and then redraw it after the page loaded and it resized the screen?). CSS is a very powerful tool that separates structure and presentation (the tags on the page are made separate from what they are supposed to look like so you don't have 10 attribute tags on every html element). So if you're using an attribute to change how something looks on the page, you're doing it wrong, and its more than likely that your page is going to stop working one of these days and I don't mean the distant future. Besides, your pages will be smaller, and you can do more cool stuff.

Even though vbscript is only supported by Internet explorer (and things based off of it), there is a mimetype format that should be applied to your script tag. (<script type="text/vbscript">). Other browsers are on the verge of pushing multiple languages as well, so its best to get in the habit early. (Some versions of Firefox support VBScript with a plugin).

Use Option Explicit at the beginning of your script, and take off On Error Resume Next. If you have it and don;t know why, you should probably take it out.

The <script> tag should only go inside the <head></head> tags or inside the <body></body> tags. Anywhere else is wrong. (This used to be different a few years ago, but there is now a standard). The script you put in the <head> tag should be code you need execute or in memory before the page is loaded. The code in the body should always be at the end. Don't use Document.write() or Document.writeline(). Use the DOM, that's what its made for.

This is far from all inclusive, and I have barely scratched the surface on best practices to follow and I have no doubt others have much more to contribute. By using some of these suggestions and more, you can be sure that once you put your program on the internet, intranet, on a CD, or anywhere else, it will still work pretty much exactly the same 10 years from now. Can you say the same thing about what you have already written?
<message edited by TNO on Wednesday, May 27, 2009 6:25 AM>
To iterate is human, to recurse divine. -- L. Peter Deutsch
#1
    ehvbs

    • Total Posts : 3312
    • Scores: 110
    • Reward points : 0
    • Joined: 6/22/2005
    • Location: Germany
    • Status: offline
    RE: Breaking Bad Habits Tuesday, April 01, 2008 12:22 AM (permalink)
    0
    Hi TNO,

    that's a very valuable/nice contribution. I agree with all of your points, except one:
    I'd put all code into the <head> (or even external files 'imported' with <script .. src = ...>)
    to avoid the mixing/messing HTML- and VBScript-Code in the <body>.

    Regards

    ehvbs

    P.S. How about the vbslint project?


    #2
      TNO

      • Total Posts : 2091
      • Scores: 34
      • Reward points : 0
      • Joined: 12/18/2004
      • Location: Earth
      • Status: offline
      RE: Breaking Bad Habits Tuesday, April 01, 2008 6:28 AM (permalink)
      0
      Well, the concept of putting some of the code in the body section was aimed more towards those programming something over the web where loading speeds are a big concern. If you have alot of code that isn't going to run until after window.onload, it may be wiser to put it at the end of the body so the rest of the page can load without waiting to read all the script first in the head and then find out it cant do anything yet anyway.

      As for the VBSLint project: I've been a bit sidetracked on my website plus getting those Script Components rewritten for VBScript. I haven't forgotten, just so much code flying around at once for me right now
      To iterate is human, to recurse divine. -- L. Peter Deutsch
      #3
        TNO

        • Total Posts : 2091
        • Scores: 34
        • Reward points : 0
        • Joined: 12/18/2004
        • Location: Earth
        • Status: offline
        RE: Breaking Bad Habits Monday, January 12, 2009 3:33 AM (permalink)
        0
        Something else I forgot to mention was Events.

        Instead of doing this:

        <button id="MyButton" onclick="Foo">Click</button>

        Do this:

        Sub MyButton_onclick
            ' Do Stuff
        End Sub

        ....

        <button id="MyButton">Click</button>


        Separating your content from your style and your logic will make things magnitudes easier to work with.

        For more examples, you can follow along in the evolution of another user's code and how it went from soupy mess to reasonably robust:
        http://www.visualbasicscript.com/m_67302/mpage_1/key_/tm.htm#67346
         
        To iterate is human, to recurse divine. -- L. Peter Deutsch
        #4
          Fredledingue

          • Total Posts : 572
          • Scores: 0
          • Reward points : 0
          • Joined: 5/9/2005
          • Location: Europe
          • Status: offline
          RE: Breaking Bad Habits Sunday, January 25, 2009 6:03 AM (permalink)
          0

          ORIGINAL: TNO

          Something else I forgot to mention was Events.

          Instead of doing this:

          <button id="MyButton" onclick="Foo">Click</button>

          Do this:

          Sub MyButton_onclick
             ' Do Stuff
          End Sub

          ....

          <button id="MyButton">Click</button>


          Separating your content from your style and your logic will make things magnitudes easier to work with.

          For more examples, you can follow along in the evolution of another user's code and how it went from soupy mess to reasonably robust:
          http://www.visualbasicscript.com/m_67302/mpage_1/key_/tm.htm#67346


           
          Except when several buttons fires the same sub.
          What I do often is using several cases in one sub to do similar things.
           
          Sub xyz(var)
          Select Case var
          Case 0
          Case 1
          End Select
          End Sub

           
          --------
           
          Question: What DOCTYPE should one use? Always the same (that one in your example) or does it change from one case to another?
           
          Question: Do you think it's a good habit to useVBScript while it's not supported in firefox and Opera? (I couldn't find the vbs plugin for Firefox)

           
          Fred
          #5
            TNO

            • Total Posts : 2091
            • Scores: 34
            • Reward points : 0
            • Joined: 12/18/2004
            • Location: Earth
            • Status: offline
            RE: Breaking Bad Habits Sunday, January 25, 2009 7:12 AM (permalink)
            0
            This is where generic events come in to play:

            Assuming I have a group of buttons:

            <button class="tab">Tab 1</button>
            <button class="tab">Tab 2</button>
            <button class="tab">Tab 3</button>


             Sub Document_onclick()
                 Dim tg : Set tg = Window.Event.SrcElement
             
                 If tg.className = "tab" Then
                     'Do Stuff
                 End If
             End Sub
             




            Question: What DOCTYPE should one use? Always the same (that one in your example) or does it change from one case to another?


            Currently you have a practical choice between 4 doctypes (HTML 5 is still in the works so its not included):

            XHTML 1.0 Strict
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

            XHTML 1.0 Transitional
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

            XHTML 1.0 Frameset
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

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

            XHTML is a stricter version of HTML (XML based markup) so your content will be much cleaner. I highly suggest using any of the XHTML doctypes.


            Question: Do you think it's a good habit to useVBScript while it's not supported in firefox and Opera? (I couldn't find the vbs plugin for Firefox)


            Without a doubt I believe you NEED to learn JavaScript. VBScript is at a dead end and is just waiting for the burial. JavaScript is going stronger than ever and is now JIT compiled in 3 major browsers. WSH JScript is  a subset of the current JavaScript version. (version 1.8.1), so your knowledge can be applied not only in a cross browser manner, but also in a massive collection of environments . Tutorials, libraries and references are near limitless plus I'm willing to answer just about any possible question on the language's syntax and semantics.
            To iterate is human, to recurse divine. -- L. Peter Deutsch
            #6
              Fredledingue

              • Total Posts : 572
              • Scores: 0
              • Reward points : 0
              • Joined: 5/9/2005
              • Location: Europe
              • Status: offline
              RE: Breaking Bad Habits Monday, January 26, 2009 12:29 PM (permalink)
              0
              Fred
              #7
                Fredledingue

                • Total Posts : 572
                • Scores: 0
                • Reward points : 0
                • Joined: 5/9/2005
                • Location: Europe
                • Status: offline
                RE: Breaking Bad Habits Sunday, March 01, 2009 12:34 PM (permalink)
                0
                When writing a vbs file, don't use "Title" as a variable. Use "MyTitle" instead so that it doesn't get broke when ported to hta.
                (hta doesn't support the variable "Title" since it's an html tag)
                Fred
                #8
                  luckystar09

                  • Total Posts : 2
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 8/20/2009
                  • Status: offline
                  Re: RE: Breaking Bad Habits Thursday, August 20, 2009 1:01 PM (permalink)
                  0
                  I'm very interested! I would love to find out more.
                  script install
                  #9
                    Fredledingue

                    • Total Posts : 572
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 5/9/2005
                    • Location: Europe
                    • Status: offline
                    Re: RE: Breaking Bad Habits Saturday, August 22, 2009 5:17 AM (permalink)
                    0
                    Use Javascript letter case convention.
                    Like that when you translate vbs to js, there are less mistakes.

                    Vbs is not case sensitive. Javascript is.
                    So type
                    createObject instead of CreateObject
                    Fred
                    #10
                      TNO

                      • Total Posts : 2091
                      • Scores: 34
                      • Reward points : 0
                      • Joined: 12/18/2004
                      • Location: Earth
                      • Status: offline
                      RE: Breaking Bad Habits Friday, September 10, 2010 9:51 AM (permalink)
                      0
                      Fredledingue


                      When writing a vbs file, don't use "Title" as a variable. Use "MyTitle" instead so that it doesn't get broke when ported to hta.
                      (hta doesn't support the variable "Title" since it's an html tag)


                      Do you have a test case for this behavior?
                      To iterate is human, to recurse divine. -- L. Peter Deutsch
                      #11
                        TNO

                        • Total Posts : 2091
                        • Scores: 34
                        • Reward points : 0
                        • Joined: 12/18/2004
                        • Location: Earth
                        • Status: offline
                        Re: RE: Breaking Bad Habits Friday, September 10, 2010 9:54 AM (permalink)
                        0
                        Fredledingue


                        Use Javascript letter case convention.
                        Like that when you translate vbs to js, there are less mistakes.

                        Vbs is not case sensitive. Javascript is.
                        So type
                        createObject instead of CreateObject


                        I assume you meant not literally of course. in JScript you don't use "CreateObject", you use "new ActiveXObject"

                        Also, the case convention is to use ProperCase when the function is a constructor, regular methods and functions are camelCase. Its important to look at the API documentation though, since most COM objects use ProperCase for everything except properties.
                        To iterate is human, to recurse divine. -- L. Peter Deutsch
                        #12
                          Fredledingue

                          • Total Posts : 572
                          • Scores: 0
                          • Reward points : 0
                          • Joined: 5/9/2005
                          • Location: Europe
                          • Status: offline
                          Re: RE: Breaking Bad Habits Friday, November 26, 2010 2:14 PM (permalink)
                          0
                          Hmm, ...ok the CreateObject was a bad example (I didn't know it was different in js).
                          An example on the DOM would be more appropriate.
                           
                          About the title in hta: That's wierd, I had the problem a long time ago, I'm sure about that. And now it looks like it disapeared. Must have been an upgrade...
                          Fred
                          #13
                            CØLLØSUS

                            • Total Posts : 21
                            • Scores: 0
                            • Reward points : 0
                            • Joined: 1/3/2011
                            • Status: offline
                            Re: Breaking Bad Habits Thursday, January 06, 2011 9:02 PM (permalink)
                            0
                            Works. Check out mine
                            #14
                              DiGiTAL.SkReAM

                              • Total Posts : 1259
                              • Scores: 7
                              • Reward points : 0
                              • Joined: 9/7/2005
                              • Location: Clearwater, FL, USA
                              • Status: offline
                              RE: Breaking Bad Habits Tuesday, March 15, 2011 9:20 AM (permalink)
                              0
                              TNO

                              Without a doubt I believe you NEED to learn JavaScript. VBScript is at a dead end and is just waiting for the burial. JavaScript is going stronger than ever and is now JIT compiled in 3 major browsers. WSH JScript is  a subset of the current JavaScript version. (version 1.8.1), so your knowledge can be applied not only in a cross browser manner, but also in a massive collection of environments . Tutorials, libraries and references are near limitless plus I'm willing to answer just about any possible question on the language's syntax and semantics.


                              The ease of use of vbs just can't be beaten by any of what i call "c-flavored" scripting languages.  jscript, python, etc.
                              I guess that it makes it easier for you real programmers to use, but for a sysadmin like me, vbs is where its at.
                              If I had to give up vbs, I'd go back to nt shell scripting combined with qbasic.  And if that didn't work... well, I'd just do thing manually and charge for every hour it takes! Muahahahahaha!
                               
                              "Would you like to touch my monkey?" - Dieter (Mike Meyers)

                              "It is better to die like a tiger, than to live like a pussy."
                              -Master Wong, from Balls of Fury
                              #15
                                DiGiTAL.SkReAM

                                • Total Posts : 1259
                                • Scores: 7
                                • Reward points : 0
                                • Joined: 9/7/2005
                                • Location: Clearwater, FL, USA
                                • Status: offline
                                Re: Breaking Bad Habits Tuesday, March 15, 2011 9:29 AM (permalink)
                                0
                                CØLLØSUS


                                Works. Check out mine


                                Yeah.  I have no idea what you are referring to here.  And I doubt that anyone else does, either.
                                 
                                Use the Quotes, Luke!
                                 
                                 
                                (that's a reference to the original Star Wars movie, wherein a character named Obi Won Kenobi said to another character named Luke Skywalker, "Use the Force, Luke!"  But I took that quote, and changed the word 'Force' to 'Quotes'.  By doing so, I intended to bring to your attention - in a humorous fashion - the Quote functionality that it built into the forum software.  By so doing, I had hoped that by using that statement so soon after stating that nobody knew what you were referring to, would strongly imply that if you quoted the message that you were replying to, it would give everyone else in the forum a frame of reference in which to read your post.  Which was my main goal in the first place, but by doing it in a somewhat humorous fashion, it would lessen the feeling of "this guy is just being a jerk and telling me to quote posts when I reply like he is better than me!"  I wanted to lessen that feeling because I do not want you to feel that way at all, since I do not feel that I am any better than you.  Well, maybe a little, but I would never be so rude as to rub your face in it in the forums.  Ok, maybe I would, but I would try to do so in a humorous fashion, so that you wouldn't realize that I was actually being serious.)
                                "Would you like to touch my monkey?" - Dieter (Mike Meyers)

                                "It is better to die like a tiger, than to live like a pussy."
                                -Master Wong, from Balls of Fury
                                #16
                                  TNO

                                  • Total Posts : 2091
                                  • Scores: 34
                                  • Reward points : 0
                                  • Joined: 12/18/2004
                                  • Location: Earth
                                  • Status: offline
                                  RE: Breaking Bad Habits Friday, March 25, 2011 2:38 PM (permalink)
                                  0
                                  DiGiTAL.SkReAM


                                  TNO

                                  Without a doubt I believe you NEED to learn JavaScript. VBScript is at a dead end and is just waiting for the burial. JavaScript is going stronger than ever and is now JIT compiled in 3 major browsers. WSH JScript is  a subset of the current JavaScript version. (version 1.8.1), so your knowledge can be applied not only in a cross browser manner, but also in a massive collection of environments . Tutorials, libraries and references are near limitless plus I'm willing to answer just about any possible question on the language's syntax and semantics.


                                  The ease of use of vbs just can't be beaten by any of what i call "c-flavored" scripting languages.  jscript, python, etc.
                                  I guess that it makes it easier for you real programmers to use, but for a sysadmin like me, vbs is where its at.
                                  If I had to give up vbs, I'd go back to nt shell scripting combined with qbasic.  And if that didn't work... well, I'd just do thing manually and charge for every hour it takes! Muahahahahaha!

                                   
                                  I'm surprised you put Python in there.
                                  To iterate is human, to recurse divine. -- L. Peter Deutsch
                                  #17
                                    bloodstaindewok

                                    • Total Posts : 8
                                    • Scores: 0
                                    • Reward points : 0
                                    • Joined: 4/22/2011
                                    • Status: offline
                                    RE: Breaking Bad Habits Friday, April 22, 2011 8:19 AM (permalink)
                                    0
                                    DiGiTAL.SkReAM

                                    I guess that it makes it easier for you real programmers to use, but for a sysadmin like me, vbs is where its at.
                                    If I had to give up vbs, I'd go back to nt shell scripting combined with qbasic.  And if that didn't work... well, I'd just do thing manually and charge for every hour it takes! Muahahahahaha!

                                    I agree digital.  When dealing with system admin operations vbs is awesome.  Getting an IT gig is what prompted me to pick up scripting again as I haven't touched it since my high school C++ class.  +1 for your business tactics and I always love a good Dr. Doom laugh.
                                    #18

                                      Online Bookmarks Sharing: Share/Bookmark

                                      Jump to:

                                      Current active users

                                      There are 0 members and 2 guests.

                                      Icon Legend and Permission

                                      • New Messages
                                      • No New Messages
                                      • Hot Topic w/ New Messages
                                      • Hot Topic w/o New Messages
                                      • Locked w/ New Messages
                                      • Locked w/o New Messages
                                      • Read Message
                                      • Post New Thread
                                      • Reply to message
                                      • Post New Poll
                                      • Submit Vote
                                      • Post reward post
                                      • Delete my own posts
                                      • Delete my own threads
                                      • Rate post

                                      2000-2012 ASPPlayground.NET Forum Version 3.8
                                      mbt shoes www.wileywilson.com