1 / 19

Javascript (Continue)

Javascript (Continue). String Date Array. String Object. Return the length of string –length() Return the postion of the first occurrence of a text in a string –indexOf Search for a text in a string and return the text if found – match() Replace characters in a string – replace().

urania
Download Presentation

Javascript (Continue)

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. Javascript (Continue) • String • Date • Array

  2. StringObject • Return the length of string –length() • Return the postion of the first occurrenceof a text in a string–indexOf • Search for a text in a string and return the text if found– match() • Replace characters in a string – replace()

  3. Return the length of string –length() • <html> • <body> • <script type="text/javascript"> • var txt="Hello World!"; • document.write(txt.length); • </script> • </body> • </html>

  4. Return the postion of the first occurrenceof a text in a string–indexOf() <html> <body> <script type="text/javascript"> var str="Hello world!"; document.write(str.indexOf("Hello") + "<br />"); document.write(str.indexOf("World") + "<br />"); document.write(str.indexOf("world")); </script> </body> </html>

  5. Search for a text in a string and return the text if found– match() <html> <body> <script type="text/javascript"> var str="Hello world!"; document.write(str.match("world") + "<br />"); document.write(str.match("World") + "<br />"); document.write(str.match("worlld") + "<br />"); document.write(str.match("world!")); </script> </body> </html>

  6. Replace characters in a string – replace() <html> <body> <script type="text/javascript"> var str="Visit Microsoft!"; document.write(str.replace(/Microsoft/,"W3Schools")); </script> </body> </html>

  7. Date Object • Use Date() to return today’s date and time • Use getTime() to calculate the year since 1970 • Use setFullyear() to set a specific date • Use getDay() and an array to write a weekday • Display a clock

  8. Use Date() to return today’s date and time <html> <body> <script type="text/javascript"> document.write(Date()); </script> </body> </html>

  9. Use getTime() to calculate the year since 1970 <html> <body> <script type="text/javascript"> var minutes = 1000*60; var hours = minutes*60; var days = hours*24; var years = days*365; var d = new Date(); var t = d.getTime(); var y = t/years; document.write("It's been: " + y + " years since 1970/01/01!"); </script> </body> </html>

  10. Use setFullyear() to set a specific date html> <body> <script type="text/javascript"> var d = new Date(); d.setFullYear(1992,10,3); document.write(d); </script> </body> </html>

  11. Use getDay() and an array to write a weekday <html> <body> <script type="text/javascript"> var d=new Date(); var weekday=new Array(7); weekday[0]="Sunday"; weekday[1]="Monday"; weekday[2]="Tuesday"; weekday[3]="Wednesday"; weekday[4]="Thursday"; weekday[5]="Friday"; weekday[6]="Saturday"; document.write("Today it is " + weekday[d.getDay()]); </script> </body> </html>

  12. Display a clock

  13. ArrayObject • Create an array • For …in and elements of an array • Join two array – concat() • Put array elements into a string – join() • Literal array –sort() • Numeric array –sort()

  14. Create an array <html> <body> <script type="text/javascript"> var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW"; for (i=0;i<mycars.length;i++) { document.write(mycars[i] + "<br />"); } </script> </body> </html>

  15. For …in Statement html> <body> <script type="text/javascript"> var x; var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW"; for (x in mycars) { document.write(mycars[x] + "<br />"); } </script> </body> </html>

  16. Join two array – concat() <html> <body> <script type="text/javascript"> var arr = new Array(3); arr[0] = "Jani"; arr[1] = "Tove"; arr[2] = "Hege"; var arr2 = new Array(3); arr2[0] = "John"; arr2[1] = "Andy"; arr2[2] = "Wendy"; document.write(arr.concat(arr2)); </script> </body> </html>

  17. Put array elements into a string – join() <html> <body> <script type="text/javascript"> var arr = new Array(3); arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; document.write(arr.join() + "<br />"); document.write(arr.join(".")); </script> </body>

  18. Literal array –sort() <html> <body> <script type="text/javascript"> var arr = new Array(6); arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[3] = "Kai Jim"; arr[4] = "Borge"; arr[5] = "Tove"; document.write(arr + "<br />"); document.write(arr.sort()); </script> </body> </html>

  19. Numeric array –sort() <script type="text/javascript"> function sortNumber(a, b) { return a - b; } var arr = new Array(6); arr[0] = "10"; arr[1] = "5"; arr[2] = "40"; arr[3] = "25"; arr[4] = "1000"; arr[5] = "1"; document.write(arr + "<br />"); document.write(arr.sort(sortNumber)); </script>

More Related