Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Breaking Bad Habits

 
Logged in as: Guest
arrSession:exec spGetSession 2,18,58515
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript Tutorial >> Breaking Bad Habits
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Breaking Bad Habits - 3/30/2008 7:31:21 AM   
  TNO


Posts: 1064
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
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:


      

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. (I 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 -- 4/1/2008 6:47:47 AM >


_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch
 
 
Post #: 1
 
 RE: Breaking Bad Habits - 3/31/2008 11:22:16 PM   
  ehvbs

 

Posts: 2077
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
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?

(in reply to TNO)
 
 
Post #: 2
 
 RE: Breaking Bad Habits - 4/1/2008 5:28:30 AM   
  TNO


Posts: 1064
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
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

(in reply to ehvbs)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript Tutorial >> Breaking Bad Habits Page: [1]
Jump to:





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
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts