1 / 37

آموزش طراحی وب سایت جلسه دهم – جاوا اسکریپت 1

آموزش طراحی وب سایت جلسه دهم – جاوا اسکریپت 1. تدریس طراحی وب برای اطلاعات بیشتر تماس بگیرید تاو شماره تماس: 09125773990 09371410986 پست الکترونیک : TargetLearning@gmail.com. Java Scripts #1. Part 10 Author :Babak Tavatav. What is JavaScript?.

toddgray
Download Presentation

آموزش طراحی وب سایت جلسه دهم – جاوا اسکریپت 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. آموزش طراحی وب سایتجلسه دهم – جاوا اسکریپت 1 تدریس طراحی وببرای اطلاعات بیشتر تماس بگیرید تاوشماره تماس: 0912577399009371410986پست الکترونیک : TargetLearning@gmail.com

  2. Java Scripts#1 Part 10 Author :Babak Tavatav

  3. What is JavaScript? • JavaScript was designed to add interactivity to HTML pages • JavaScript is a scripting language • A scripting language is a lightweight programming language • JavaScript is usually embedded directly into HTML pages • JavaScript is an interpreted language (means that scripts execute without preliminary compilation) • Everyone can use JavaScript without purchasing a license

  4. Put a JavaScript into an HTML page • Like CSS

  5. <html> <body> <script type="text/javascript"> document.write("Hello World!"); </script> </body> </html>

  6. <html> <body> <script type="text/javascript"> document.write("<h1>Hello World!</h1>"); </script> </body> </html>

  7. Scripts in <head> <html><head><script type="text/javascript"> function message() { alert("This alert box was called with the onload event"); }</script></head><body onload="message()"></body></html>

  8. Scripts in <body> <html> <head> </head> <body> <script type="text/javascript"> document.write("This message is written by JavaScript"); </script> </body> </html>

  9. Using an External JavaScript <html> <head> </head> <body> <script type="text/javascript" src="xxx.js"> </script> <p> The actual script is in an external script file called "xxx.js". </p> </body> </html>

  10. JavaScript Comments <html> <body> <script type="text/javascript"> // Write a heading document.write("<h1>This is a heading</h1>"); // Write two paragraphs: document.write("<p>This is a paragraph.</p>"); document.write("<p>This is another paragraph.</p>"); </script> </body> </html>

  11. JavaScript Multi-Line Comments <script type="text/javascript">/*The code below will writeone heading and two paragraphs*/document.write("<h1>This is a heading</h1>");document.write("<p>This is a paragraph.</p>");document.write("<p>This is another paragraph.</p>");</script>

  12. JavaScript Variables

  13. JavaScript Arithmetic Operators

  14. JavaScript Assignment Operators

  15. Example <html> <body> <script type="text/javascript"> x=5+5; document.write(x); document.write("<br />"); x="5"+"5"; document.write(x); document.write("<br />"); x=5+"5"; document.write(x); document.write("<br />"); x="5"+5; document.write(x); document.write("<br />"); </script> <p>The rule is: If you add a number and a string, the result will be a string.</p> </body> </html>

  16. Comparison OperatorsGiven that x=5

  17. Logical Operatorsx=6 and y=3

  18. Conditional Operator variablename=(condition)?value1:value2  greeting=(visitor=="PRES")?"Dear President ":"Dear ";

  19. Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In JavaScript we have the following conditional statements: if statement - use this statement to execute some code only if a specified condition is true if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement - use this statement to select one of many blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed

  20. If...else Statement Syntax if (condition)  {  code to be executed if condition is true  }else  {  code to be executed if condition is not true  }

  21. <html> <body> <script type="text/javascript"> var d = new Date(); var time = d.getHours(); if (time < 10) { document.write("<b>Good morning</b>"); } else { document.write("<b>Good day</b>"); } </script> <p> This example demonstrates the If...Else statement. </p> <p> If the time on your browser is less than 10, you will get a "Good morning" greeting. Otherwise you will get a "Good day" greeting. </p> </body> </html>

  22. if (condition1)  {  code to be executed if condition1 is true  }else if (condition2)  {  code to be executed if condition2 is true  }else  {  code to be executed if condition1 and condition2 are not true  }

  23. <html> <body> <script type="text/javascript"> var d = new Date(); var time = d.getHours(); if (time<10) { document.write("<b>Good morning</b>"); } else if (time>=10 && time<16) { document.write("<b>Good day</b>"); } else { document.write("<b>Hello World!</b>"); } </script> <p> This example demonstrates the if..else if...else statement. </p> </body> </html>

  24. The JavaScript Switch Statement switch(n){case 1:  execute code block 1  break;case 2:  execute code block 2  break;default:  code to be executed if n is different from case 1 and 2}

  25. <html> <body> <script type="text/javascript"> var d = new Date(); theDay=d.getDay(); switch (theDay) { case 5: document.write("<b>Finally Friday</b>"); break; case 6: document.write("<b>Super Saturday</b>"); break; case 0: document.write("<b>Sleepy Sunday</b>"); break; default: document.write("<b>I'm really looking forward to this weekend!</b>"); } </script> <p>This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.</p> </body> </html>

  26. Alert Box alert("sometext");

  27. <html> <head> <script type="text/javascript"> function show_alert() { alert("Hello! I am an alert box!"); } </script> </head> <body> <input type="button" onclick="show_alert()" value="Show alert box" /> </body> </html>

  28. Confirm Box

  29. <html> <head> <script type="text/javascript"> function show_confirm() { var r=confirm("Press a button!"); if (r==true) { alert("You pressed OK!"); } else { alert("You pressed Cancel!"); } } </script> </head> <body> <input type="button" onclick="show_confirm()" value="Show a confirm box" /> </body> </html>

  30. Prompt Box A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. Syntax prompt("sometext","defaultvalue");

  31. <html> <head> <script type="text/javascript"> function show_prompt() { var name=prompt("Please enter your name","Harry Potter"); if (name!=null && name!="") { document.write("Hello " + name + "! How are you today?"); } } </script> </head> <body> <input type="button" onclick="show_prompt()" value="Show prompt box" /> </body> </html>

  32. JavaScript Functions To keep the browser from executing a script when the page loads, you can put your script into a function. A function contains code that will be executed by an event or by a call to the function. You may call a function from anywhere within a page (or even from other pages if the function is embedded in an external .js file). Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that a function is read/loaded by the browser before it is called, it could be wise to put functions in the <head> section.

  33. How to Define a Function function functionname(var1,var2,...,varX){some code}

  34. <html> <head> <script type="text/javascript"> function displaymessage() { alert("Hello World!"); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> <p>By pressing the button above, a function will be called. The function will alert a message.</p> </body> </html>

  35. The return Statement

  36. <html> <head> <script type="text/javascript"> function product(a,b) { return a*b; } </script> </head> <body> <script type="text/javascript"> document.write(product(4,3)); </script> <p>The script in the body section calls a function with two parameters (4 and 3).</p> <p>The function will return the product of these two parameters.</p> </body> </html>

  37. The END

More Related