Java Script


Published on

Presentation delivered to magnet staff

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java Script

  1. 1. JavaScript
  2. 2. With JavaScript, <ul><li>A Web page can really react to what you're doing. </li></ul><ul><li>Images can swap when you move a cursor over them. </li></ul><ul><li>Form elements can influence each other on the fly. </li></ul><ul><li>And calculations can be made without having to resort to a CGI script. </li></ul><ul><li>There's none of this submit-and-wait stuff - everything happens on your Web page while you're playing with it. </li></ul>
  3. 3. Why JavaScript <ul><li>One of the best things about JavaScript is that you can do a great deal with very little programming. </li></ul><ul><li>You don't need a fancy computer, you don't need any software other than a Web browser, and you don't need access to a Web server; you can do all your work right on your own computer. </li></ul><ul><li>Even though it's simple to work with, JavaScript is a complete programming language, so as you learn more complicated JavaScript, you're also learning the basics of computer programming. If you want to move on to other programming languages, like Perl, C, C++, or Java, JavaScript is a great introduction. </li></ul>
  4. 4. What is Java Script? <ul><li>JavaScript is not Java. </li></ul><ul><li>Sometimes JavaScript isn't JavaScript! It turns out that different browsers deal with JavaScript differently. </li></ul><ul><li>View Source! The best way to learn JavaScript is to look at scripts other people have written. JavaScript, just like HTML, can be viewed by selecting View Source on your browser. Do it frequently! </li></ul><ul><li>Finally, as with HTML, the best way to learn JavaScript is to experiment freely and often. </li></ul>
  5. 5. JavaScript Syntax <ul><li>JavaScript usually goes between the </title> tag and the </head> tag. </li></ul><ul><li>Like HTML, JavaScript is just text that can be typed into a word processor. It goes right into the HTML that describes your page. (Although I've only shown JavaScript in the header of the HTML page, you can also put JavaScript in the body. You'll see an example of this in the next lesson.) </li></ul><ul><li>The beginning of a bit of JavaScript begins with <script language=&quot;JavaScript&quot;> </li></ul>
  6. 6. Your First JavaScript Exercise <ul><li><HTML> </li></ul><ul><li><HEAD> </li></ul><ul><li><TITLE> </li></ul><ul><li></TITLE> </li></ul><ul><li><SCRIPT LANGUAGE = &quot;JavaScript&quot;> alert (&quot;Welcome to my Web Site&quot;); </SCRIPT> </li></ul><ul><li></HEAD> </li></ul><ul><li><BODY> </li></ul><ul><li>My first page with JavaScript Code. </li></ul><ul><li></BODY> </li></ul><ul><li></HTML> </li></ul>
  7. 7. Examples <ul><li>alerts </li></ul><ul><ul><li><A HREF=&quot;javascript:alert('Hello World')&quot;>Hello World</A> </li></ul></ul><ul><ul><li><A HREF=&quot;javascript:alert('Hello World');alert('Hello Net')&quot;>Hello World</a> </li></ul></ul><ul><li>Variables </li></ul><ul><ul><li>var monkey_love = prompt(&quot;Do you love the monkey?&quot;,&quot;Type yes or no&quot;); </li></ul></ul><ul><ul><li>if (monkey_love == &quot;yes&quot;) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>alert(&quot;Welcome! I'm so glad you came! Please, read on!&quot;); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Events </li></ul><ul><ul><li><a href=&quot;#&quot; onClick=&quot;alert('Ooo, do it again!');&quot;>Click on me!</a> </li></ul></ul><ul><ul><li><a href=&quot;#&quot; onMouseOver=&quot;alert('Hee hee!');&quot;>Mouse over me!</a> </li></ul></ul>
  8. 8. MouseOver <ul><li>One of the most commonly used features of JavaScript is the ability to change images on a mouseover. </li></ul><ul><ul><li><img src=&quot;thumbs_up1.gif&quot; name=&quot;the_image&quot;> </li></ul></ul><ul><ul><li><a href=&quot;#&quot; onMouseOver=&quot;document.the_image.src='thumbs_up2.gif';&quot;> Change </a> </li></ul></ul><ul><li>This is just like a standard <img src= > tag except this one has been given a name: the_image . This name could be anything: my_image, a_box, whatever - but it can't have any spaces in it. </li></ul>
  9. 9. Introduction to Window Manipulation <ul><li>Before learning how to open a window in JavaScript, you should know how to open one using HTML. The HTML used to do this is: </li></ul><ul><li><a href=&quot;yer_new_window.html&quot; target=&quot;yer_new_window&quot;>this link</a> </li></ul><ul><li>The important thing to know about windows opened by targeted links is that the window above now has the name &quot;yer_new_window&quot; associated with it. If you have another href that uses &quot;yer_new_window&quot; as the target, and you haven't closed the window yet, whatever URL you put in that link will open in the original window. </li></ul><ul><li>The JavaScript syntax is: </li></ul><ul><li>;URL&quot;,&quot;name&quot;,&quot;features&quot;); </li></ul><ul><li>And the code that will use the above syntax will look like this </li></ul><ul><li><a href=&quot;#&quot; onClick=&quot;'index.html','india');&quot;> </li></ul><ul><li>Here's a window named javascript_1</a> </li></ul>
  10. 10. Windows Features Part I <ul><li>The third parameter of the method is a list of features that you'd like your window to have. </li></ul><ul><li>If you don't include this parameter at all, the window will contain all the features of a default browser window. However, if you specify any features in the third parameter, just those features will appear. </li></ul><ul><li>For example, if you write </li></ul><ul><li>;some_url&quot;,&quot;window_name&quot;,&quot;location,menubar&quot;); </li></ul><ul><li>you'll get a window with just the location box (also called address bar - the place in your browser where you type in a URL) and a menu bar (File, Edit, etc.). Note that it's important that you don't put any spaces in the string. </li></ul><ul><li>Another example is </li></ul><ul><li>;some_url&quot;,&quot;window_name&quot;,&quot;location,height=100,width=100&quot;); </li></ul><ul><li>This will open a window that is 100 pixels high and 100 pixels wide and has no features other than a location field. </li></ul>
  11. 11. Windows Features Part II <ul><li>Here's a list of the features that you can include in the feature string: </li></ul><ul><li>Menubar </li></ul><ul><ul><li>It includes File, Edit, and a few other items. </li></ul></ul><ul><li>Status </li></ul><ul><ul><li>This is the message bar at the bottom of your window. </li></ul></ul><ul><li>Scrollbars </li></ul><ul><ul><li>This allows scrollbars to appear when necessary. · </li></ul></ul><ul><li>Resizable </li></ul><ul><ul><li>If resizable is listed, the window can be resized. </li></ul></ul><ul><li>Width </li></ul><ul><ul><li>The width of the window in pixels. · </li></ul></ul><ul><li>Height </li></ul><ul><ul><li>The height of the window in pixels. </li></ul></ul><ul><li>Toolbar </li></ul><ul><ul><li>The browser toolbar, which contains the Back and Forward buttons. </li></ul></ul><ul><li>Location </li></ul><ul><ul><li>The address bar of a browser into which you can type URLs. </li></ul></ul>
  12. 12. Exercise 1 <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><SCRIPT LANGUAGE = &quot;JavaScript&quot;> </li></ul><ul><li>var monkey_love = prompt (&quot;Do you love monkey?&quot;, &quot;Type Yes or No&quot;); </li></ul><ul><li>if (monkey_love == &quot;yes&quot;){ </li></ul><ul><li>alert (&quot;Welcome! I am glad you came! Please, read on!&quot;); </li></ul><ul><li>}else { </li></ul><ul><li>alert (&quot;Go away&quot;); </li></ul><ul><li>} </li></ul><ul><li></SCRIPT> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul>
  13. 13. Exercise 2 <ul><li><SCRIPT LANGUAGE = &quot;JavaScript&quot;> </li></ul><ul><li>// document.writeln(&quot;Your Name is &quot;, Your_Name); </li></ul><ul><li></SCRIPT> </li></ul><ul><li><P> </li></ul><ul><li><a href=&quot;#&quot; onClick=&quot;alert('Ooo, do it again!');&quot;>Click on me!</a> </li></ul><ul><li><P> </li></ul><ul><li><a href=&quot;#&quot; onMouseOver=&quot;alert('Hee hee');&quot;>Mouse over me</a> </li></ul><ul><li><P> </li></ul><ul><li><a href=&quot;contact.html&quot; target=&quot;my_window&quot;>this link</a> </li></ul><ul><li><p> </li></ul><ul><li><a href=&quot;#&quot; onClick=&quot;'testit.htm','my_window', 'scrollbars,resizable,location,height=100,width=100')&quot;;>Clcik here</a> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  14. 14. Exercise 3 - Where has that browser been? <ul><li><SCRIPT language=&quot;JavaScript&quot;> function cameFrom(where) {    if (!document.referrer && !where) return true;    else return (document.referrer.indexOf(where)>=0)    } if (cameFrom(&quot;;)) {    location.replace(&quot;newpage1.html&quot;);    } else if (cameFrom(&quot;;)) {    location.replace(&quot;newpage2.html&quot;);    } if (cameFrom(&quot;;)) {    alert(&quot;You found us!&quot;);    } else if (cameFrom(&quot;;)) {    location.replace(&quot;reprimand.html&quot;);    } else if (cameFrom()) {    alert(&quot;The wind blows where it will, and you hear the sound of it, but you do not know from whence it comes.&quot;)    } </li></ul><ul><li></SCRIPT> </li></ul>
  15. 15. Hard Return in JScript <ul><li>While hard returns are irrelevant to HTML parsing, hard returns mean a world of difference in JavaScript. </li></ul><ul><li>For comments you can use the C-style block comment ( /* .... */ ) </li></ul><ul><li>Don’t use the single line comment set-off by two slashes ( // ) </li></ul><ul><li>Because it will turn everything until the closing script tag ( </script> ) into a single long comment (and will probably cause an error in the script). </li></ul>
  16. 16. Inline JavaScript <ul><li><H1 onMouseOver=&quot;'tan';&quot; onMouseOut=&quot;'purple';&quot;>Mouse over me to see my over color. Then mouse out to see my out color.</H1> </li></ul>