People who have done a little programming have no doubt written a method. Whether it is called a function or a method, is usually based on the language being used. JavaScript uses “function” for its naming convention. A function is essentially a container for lines of code to be executed when the function is called. Functions can be called when a webpage first loads up or as a response to a different page event, such as a button click.
The two places of the HTML page to put JavaScript code is in the “head” section and the “body” section. The best place to put functions in a webpage is in the “head” section, placement here ensures that the code is pre-loaded when the webpage is opened. This avoids any potential conflicts with referencing functions that have not yet been loaded into memory.
A general function would be something like:
function multiply(var a, var b)
{
return a*b;
}
A simple example of a function call. Two numbers are passed to the function and the result is written to the webpage. In most real world cases the number input would be entered by the user.
<html><head>
function multiply(var a, var b)
{
return a*b;
}
</head>
<body>
<script type=”text/javascript”>
var x = 8;
var y = 7;
document.write(multiply(x,y)));
</script>
</body></html>
383e644e-0c86-4f7e-8d19-58962e0bc8b3|0|.0
The highlight of using JavaScript is that webpage content can be generated can be created dynamically, as opposed to being completely hard coded. One such example is that of the “write” function. The webpage in many cases will be referred to as the “document” object.
<html>
<body>
<script type="text/javascript">
document.write(<h2>The Title</h2>);
document.write(<p>The Content</p>);
</script>
</body>
</html>
When the webpage loads, the html tags and their contents will be written into the page as if they were hardcoded into the page. Yes, the example is a bit goofy, but it is simplistic for this purpose. The uses of dynamic content can span into many fields of the website. Based upon a user’s answers or preferences a whole different block of text could be shown. This can be based on the user’s browser, age, gender, geographical location, and other such details.
If the sex of the user is known, it could be used to change a reference to gender based words accordingly. Adding a “him” or “her” to the right positions adds an extra layer of personalization to a webpage. This gender personalization could also expand into the photos and advertising shown when they are viewing the webpage. The same customizations will follow for age and geographical location. The marketing purposes of this simple coding can go along way.
This practice has already been in use for a long time now. Did you ever notice the ads on Facebook that refer to the city you live in? That is because they know where you live, or the general area anyways. And since you have an account there, they know specifics about you. It would be safe to assume that the ads guys see are not the same ads the young ladies see. They tailor the ads to what they believe your interests are.
90f8f97e-1bde-438b-b6a4-612eb1e1c13d|0|.0
The most important thing to know about using JavaScript is where to place it in your webpage. Ultimately this will be based on what your code does and when it needs to trigger. The two places in a website that JavaScript can be placed is the head section and the body section. If you want the scripts to run when the page is loaded place your script code in the “body” section of your website. If you are creating functions that need to be called or triggered by event, this code will go in the head section. It will load when the page loads, but must wait for a function call to activate it. You can use both header scripts and body scripts in your page. There is no limit on the amount of JavaScript your page can contain.
A basic function call would be something as seen below. The JavaScript code is in the head section and does not run until it is called by the onload event of the body section.
<html>
<head>
<script type="text/javascript">
function showAnAlertBox()
{
alert("The alert() is a built in method that displays a message box.");
}
</script>
</head>
<body onload="showAnAlertBox()">
</body>
</html>
You can also save your script code into a text document and refer to it in you webpage. This would be a good way to cut down redundant code and keep the code readable.
<html>
<head>
<script type="text/javascript" src="functions.js"></script>
</head>
<body>
</body>
</html>
534e11d0-151b-4608-83e6-03569cc4876b|0|.0
A Beginning Look at JavaScript.
JavaScript has been in use almost as long as HTML itself. The main use of JavaScript is to add some dynamic behavior to websites, which means seeing results without having to wait for a page to refresh itself every time there is a change.
The main use of JavaScript in web pages adds the ability to:
- Dynamically generate HTML code into your webpage.
- Setup responses to events, like page loads and user clicks.
- Form Validation—JavaScript can code can check the information in the input fields is present and in the correct format.
- JavaScript can detect what web browser you are using and change to a webpage made for that browser. This used to be more important than it is now.
- JavaScript can be used in the passage and storage of information in cookies. This is usually the result of filling out a web form.
To use JavaScript, one embeds scripting code into the HTML code of their web page. A basic example would be that of dynamically writing test. Normally this would be apart of an function called on a page load or if a button was clicked, but for the purpose of the example, this code is just in the body of the page and will run when the page is first loaded up.
<html>
<body>
<script type= “text/javascript”>
document.write(“document is the current web page being viewed. The red text is just for effect”);
</script>
</body>
</html>
c35062bb-77a3-4cd0-bfec-0bd019579944|0|.0
Many times Java and JavaScript get referred to as if they are the same language. But the main thing they really share is a name and some similar syntax usage. Java is a fully functional object oriented programming language, while JavaScript is a scripting language than only runs within web browsers.
One of the main draws of Java is that it is platform independent. Java code is executed through the Java Virtual Machine and not in the specific systems machine code. This means code can be written once and executed on any machine that can run the Java Virtual Machine. For example in the past, if you wanted to use a Mac program on a Windows PC, you would have to rewrite the program and recompile it on a Windows PC.
In addition to being run on the main computer console, Java programs can be run in web browsers in the form of Applets.
Netscape as a way to enhance the HTML elements present on web pages created JavaScript. JavaScript does not have typed elements as most traditional programming languages do. In JavaScript everything is treated as an object, there are no separate types to differentiate text and numbers. JavaScript is widely used to collect information from Web Forms and to create and manipulate cookies.
So yes, while some of the syntax and formatting look similar, they are not the same thing.
cc36215d-ce46-4a0a-979b-3cfb01c4c766|0|.0
XML
XML stands for Extensible Markup Language. XML is a markup language similar to HTML, but designed for a different purpose. While HTML was for made for displaying data and content, XML was created as a way to store data and content. XML is not code and does not do anything but provide a structure to determine how a collection of data will be stored. Unlike the predefined HTML tags, in XML you create your own tags.
XML is meant to be used with HTML when you need to dynamically display data on your websites. Instead of having to constantly edit the HTML of your page each time it changes, all of the data will be handled by the XML files. You can manipulate the XML files with JavaScript, which will manage the data and the HTML code will only have to deal with the display of the data.
XML is a text based format, so it maintains the quality of being software and hardware independent. Using XML to store data frees us from problem of incompatibility of different database formats. XML files can be freely shared across different types of computers.
XML is formatted in a tree like pattern that has a starting point that then branches off into leave points. Or it could also be defined as a parent child model. Like HTML, each tag must be properly nested , opened, and closed. XML tags are also case sensitive.
An example would be:
<start>
<name></name>
<address></address>
</start>
There is much more you can do with XML , this is just a basic look at the subject.
02d90bc2-4b1f-49d7-9be9-2c4ed7db245a|0|.0
AJAX is an acronym for Asynchronous JavaScript and XML. It isn’t a
programming language in itself , but is comprised of other existing technologies.
These technologies being HTML, Cascading Style Sheets (CSS), JavaScript,
the Document Object Model (DOM), XML , XSLT , and the XMLHttpRequest object.
With all of these components combined the creation of dynamic user interfaces on websites and web applications much easier. Since AJAX is asynchronous you can instantly update selected portions of the web pages rather than having to refresh the page each time a change has occurred. It can do this because rather than having a HTTP request that just goes back and forth from the website to the server, the AJAX engine becomes an intermediary between the two. The AJAX engine passes all server requests through JavaScript with the XMLHttpRequest object, while the AJAX engine itself handles all other request that don’t need to access the server.
To break down the process HTML and CSS are still used to format and stylize the webpage, while JavaScript is used to access the Document Object Model which causes the displayed information to become more dynamic and interactive. The browser and server interaction is passed through the XMLHttpRequest object. And the data sent
back to the browser is commonly formatted with XML.
43a91136-8784-4a49-9429-75a58ef74bfb|0|.0