Javascript Basics 01


JavaScript adds simple or sophisticated interactivity to a Web site, enhancing the user's experience. Like any programming language, you need to understand the building blocks before you can start programming.

Start at the Beginning

Browsers know to interpret Web pages as HTML because of the tags. Since JavaScript is contained inside an HTML document, it needs to be set apart with the tags.



TITLE< itle><br/><br/><script language"JavaScript"><! Comment from older browsers<br/><br/>YOUR SCRIPT HERE<br/><br/>// end commenting out ><br/></script><br/></head><br/><body><br/><br/>Don't forget that last </script> tag! Abrowser will try and interpret the whole HTML page as JavaScript, until it comes to that closing tag. Without it, the page will generate unsightly errors and not load properly.<br/><br/>Comment, Comment, Comment<br/><br/>Commenting code allows you, or someone else looking at it, to understand what's occuring in the code. Commenting can be done in both single and multi-line variations:<br/><br/>// single line comments<br/><br/>/ multi-line<br/>comments /<br/><br/>But what about the HTML comment <! > inside the script tags. That exists so older browsers that don't understand JavaScript won't try and interpret it. Otherwise, the code will render the page as HTML, resulting in the page displaying incorrectly.<br/><br/>Defining Variables<br/><br/>JavaScript, like all programming languages, uses variables to store information. These variables can store numbers, strings, variables that have been previously defined, and objects. For example:<br/><br/>Numeric: var x 0; <br/>String: var y "hello"; <br/>Variables: var z x + y; <br/>Object: var myImage new Image(); <br/><br/>Strings MUST contain "" around the word or phrase. Otherwise the JavaScript will interpret it as a number. Numbers and previously defined variables, likewise, should not have "" unless you want that number to be treated as a string.<br/><br/>Ex: var x hello wrong<br/><br/>Variables that store numbers and strings can be combined in a new variable. However, if anything is combined with a string, it is automatically be treated as a string. <br/><br/>Ex: var y "1";<br/>var z "2";<br/>var a y + z;<br/><br/>The variable "a" in this instance is "12" not 3, since the two strings were combined together as text, not added like numbers. This would be true even if y 1.<br/><br/>Making a Statement<br/><br/>Notice the semi-colons (;) at the end of each line of code? The semi-colon denotes the end of that particular statement. While JavaScript can sometimes be forgiving if you don't include the semi-colon at the end of each statement, it's good practice to remember to do so. Otherwise, you might not remember to put it there when you really need it.<br/><br/>Alert! Alert!<br/><br/>Alerts are one of the greatest functions in JavaScript. They not only pass information on to visitors, but help you when you're trying to hunt down a bug in your code. <br/><br/>Examples of alerts<br/><br/>alert("this is a string"); <br/>creates an alert that will contain the text"this is a string" <br/><br/>alert(x) <br/>creates an alert that will contain the value defined in variable x (make sure that variable x is defined before calling it)<br/><br/>alert("the number is " + x); <br/>-creates an alert that will combine text and the value in x.<br/><br/>It Can Do Math Too?<br/><br/>JavaScript wouldn't be much of a programming language if it couldn't do simple math. While there are dozens of complex, built-in math functions, here are the basic symbols you'll need to know:<br/><br/>addition + <br/>subtraction - <br/>multiplication <br/>division / <br/>greater than > <br/>less than < <br/>greater than or equal > <br/>less than or equal < <br/><br/>What "If" Statements<br/><br/>"If" statements are often used to compare values. If the statement is true, a set of instructions enclosed in {} executes. Comparisons like this are done using the following symbols:<br/><br/>equals <br/>not equal ! <br/><br/>In the example below, you can see how an "if" statement is used to determine text that displays in an alert window. Copy and paste the following script into an HTML page to see it at work.<br/><br/><script language"JavaScript"><br/><!-commenting from old browsers<br/>var x 6;<br/>var y 3;<br/><br/>// if statement for first alert<br/>if ( x 6 )<br/>{<br/>alert("x is equal to 6");<br/>}<br/>else<br/>{<br/>alert("x is not equal to 6");<br/>}<br/><br/>// defining variable for second alert<br/>var z x + y;<br/>alert(z);<br/>// end commenting ></script><br/><br/>Notice how the instructions for each statement (both the "if" and "else") are enclosed in {}(curly brackets). All curly brackets must have a beginning and ending, just like HTML tags. If a curly bracket is missing the JavaScript will return an error, so be sure to proof your code. <br/><br/>This document is provided for informational purposes only, and i-Netovation.com makes no warranties, either expressed or implied, in this document. Information in this document is subject to change without notice. The entire risk of the use or the results of the use of this document remains with the user. The example companies, organizations, products, people, and events depicted herein are fictitious. No association with any real company, organization, product, person, or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, wit hout the express written permission of i-Netovation.com.<br/><br/>If you believe that any of these documents, related materials or any other i-Netovation.com content materials are being reproduced or transmitted without permission, please contact: violation@i-netovation.com.<br/><br/>The names of actual companies and products mentioned herein may be the trademarks of their respective owners. <br/><p> About the Author <p>(C) Copyright 2002. Lisa Spurlin</div><div class="panel panel-default" style="padding: 10px 10px 10px 10px;"><a class="btn btn-outline-primary btn-sm" href="/on/development/">development</a> <a class="btn btn-outline-primary btn-sm" href="/on/javascript/">javascript</a> <a class="btn btn-outline-primary btn-sm" href="/on/alert/">alert</a> <a class="btn btn-outline-primary btn-sm" href="/on/statement/">statement</a> <a class="btn btn-outline-primary btn-sm" href="/on/document/">document</a> </div> </div> </div> <!-- /#page-content-wrapper --> </div> <!-- /#wrapper --> <footer class="footer"> <div class="container-fluid text-right"> <p class="text-muted"><i class="fa fa-copyright" aria-hidden="true"></i> <a href="http://ondapc.com/" target="_blank">ondapc</a> project 2024</p> </div> </footer> <!-- CONTACT US --> <div class="modal" id="contact"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title"><i class="far fa-envelope"></i> Contact Me</h4> <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button> </div> <div class="modal-body"> <form id="contactar" name="contactar" method="post"> <input type="hidden" name="token" value="0f83b1a020207f39b769ed9fd1745124"> <input type="hidden" name="captcha" value=""> <div class="form-group"> <div class="input-group-prepend"> <span class="input-group-text"><i class="fas fa-user"></i></span> <input class="form-control" id="name" name="name" placeholder="Your Name" required="required" value="" type="text"> </div> </div> <div class="form-group"> <div class="input-group-prepend"> <span class="input-group-text"><i class="far fa-envelope"></i></span> <input class="form-control" id="email" name="email" placeholder="Email" required="required" value="" type="email"> </div> </div> <div class="form-group"> <div class="input-group-prepend"> <span class="input-group-text"><i class="fas fa-edit"></i></span> <textarea name="message" id="message" class="form-control" rows="9" cols="25" placeholder="Write a message" required="required"></textarea> </div> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-primary">Send <i class="fas fa-chevron-right"></i></button> </div> </div> </div> </div> <div id="contact" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button> <h4 class="modal-title"><i class="fa fa-envelope-o"></i> Contact Me</h4> </div> <div class="modal-body"> </div> </div> </div> <script> $(function(){ $('#contactar').on('submit', function(e){ e.preventDefault(); $.ajax({ url: '/contactx/', type: 'POST', data: $('#contactar').serialize(), success: function(data){ $('#contactar')[0].reset(); $("#contact-success").append('<span><i class="fa fa-check"></i> Message Sent!</span>'); } }); }); }); </script> <!-- Bootstrap core JavaScript --> <script src="vendor/jquery/jquery.min.js"></script> <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script> <!-- Menu Toggle Script --> <script> $("#menu-toggle").click(function(e) { e.preventDefault(); $("#wrapper").toggleClass("toggled"); }); </script> </body> </html> <!-- CACHE 67c2610a9a9699ee5a2431abb6c5099a 2024-12-22 05:44:45 -->