Here are some good practices to follow for Javascript. Remember that when you are using any programing language or scripting language when you define variables try to use a meaningful name, remember to put some internal documentation like I did to help explain to you what the code is doing, becuase 6 months later it save you a heck of allot of time when you are trying to remember or figure out how everything works. When you develop other projects or your own and you leave them to develop other things you loose track and having internal documentation will help.
Remember to close your variables with a semicolon. Declatartions like statements, end with a semicolon and ca be split over several lines with each vfariable in the declaration separated by a comma. To declare multiple Variables in javascript try this:
<script type = "text/javascript">
<!--
var super, slick, and, lots, of, diff, names;
// -->
<script />
</script>
August 30th, 2005
Prompt and alert boxes can be used in Webpages to add input from users. This turtorial and script builds upon knowleadge which you would of got if you read all the javascript articles This script uses another predefined dialog box from the window object which is in this case the prompt dialog box which allows the user to input a value that the script can use. The program in this cases asks the user to input a name, the name is stored as a variable, it then echos or prints the name out using Javascript.
You can learn more about this by clicking here or here
For this we are going to be using a XHTML Strict Doc Type
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="UTF-8" />
<title>Fabio DRN tutorial on Prompt and Alert Boxes</title>
<script type="text/javascript">
<!--
var username //username is a variable set by the user
// this will make username variable equal to whatever the window prompt box input value
username = window.prompt("Please type in your name, "")
// try this
// username = window.prompt("Please type in your name, "and this will equal the default field so it will say something")
// javascript will then write it in, it works top to bottom sequentially
document.writein("<h1>" + username + " welcome to FabioDRN< /h1>"
// -->
</script>
</head>
<body>
</body>
</html>
August 30th, 2005
Avoid Using JavaScript Writing Titles
You may of seen this example in one of the previous articles.
<title><script type="text/javascript">
document.write("Printing JavaScript Titles Using
JavaScript"); </script></title>
I do not recommend this because when you look in a search engine your results come up like this;
document.write(' '); var MYHAWEMEA_LANG=""; var ...
document.write(''); else document.write(''); function
showpage(strpage) {
window.open(myhaeurl ...
else if(MYHAWEMEA_SHOWFAQLINK == "PM"){ document.write(' ...
Use server side scripting like PHP do to the same thing
You Could Do the same thing with this code.
echo('<head>' '<title>Printing JavaScript Titles Using
JavaScript</title>');
</head>
The code is much smaller and the source code isn’t made available for the user because it’s generated by the server.
The same problems go for meta elements. You can find many sites which come up by search engines with this problem. Though now some search engines such as Google have advanced they still do not generate everything for their own reasons.
Alternative using JavaScript
You Could Try Doing Using This
document.write("<title>Printing JavaScript Titles Using JavaScript
</title>");
Why use this Because the search engine can still get information out of the document.
The Previous Way
The SearchBot May See All the Scripting
...<title><script type="text/javascript">document.write('This Is What The Search Engine Sees')</script></title>...
Alternative Method
The SearchBot Will Just Pull Out Whats Between The Title Tags
...<script type="text/javascript">document.write('<title>'This Is What The Search Engine Sees</title>')</script>...
At least using the alternative method your site is better exposed to more primative search bots.
August 29th, 2005