Posts
CHAPTER - 18
- Get link
- X
- Other Apps
String in Javascript The String object is used for storing and manipulating text. A string simply stores a series of characters like "John Doe". A string can be any text inside quotes. You can use single or double quotes: Example: var carname="My Name is Khan"; You can access each character in a string with its position Example: var character=carname[7]; String Length <!DOCTYPE html> <html> <body> <script> var txt = "Hello World!"; document.write("<p>" + txt.length + "</p>"); var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; document.write("<p>" + txt.length + "</p>"); </script> </body> </html> Finding a String in a String The indexOf() method returns the position (as a number) of the first found occurrence of a specified text inside a string: Example var str="Hello world, welcome to the universe welcome."; var n=str.indexOf("welcome...
CHAPTER - 17
- Get link
- X
- Other Apps
Date and Time The Date object has been created, and now we have a variable that holds the current date! To get the information we need to print out, we have to utilize some or all of the following functions: getTime() - Number of milliseconds since 1/1/1970 @ 12:00 AM getSeconds() - Number of seconds (0-59) getMinutes() - Number of minutes (0-59) getHours() - Number of hours (0-23) getDay() - Day of the week(0-6). 0 = Sunday, ... , 6 = Saturday getDate() - Day of the month (0-31) getMonth() - Number of month (0-11) getFullYear() - The four digit year (1970-9999) <h4>It is now <script type="text/javascript"> <!-- var currentTime = new Date() var hours = currentTime.getHours() var minutes = currentTime.getMinutes() if (minutes < 10){ minutes = "0" + minutes } document.write(hours + ":" + minutes + " ") if(hours > 11){ document.write("PM") }else{ document.write("AM") } //--> </script> </h4>
CHAPTER - 16
- Get link
- X
- Other Apps
Javascript Redirect <html> <head> <script type="text/javascript"> function delayer(){ window.location = "http://www.google.com" } </script> </head> <body onLoad="setTimeout('delayer()', 5000)"> <h2>Prepare to be redirected!</h2> <p>This page is a time delay redirect, please update your bookmarks to our new location!</p> </body> </html>
CHAPTER - 14
- Get link
- X
- Other Apps
Confirm in javascript <html> <head> <script type="text/javascript"> function confirmation() { var answer = confirm("Do you want to leave this page?") if (answer==true){ window.location = "http://www.google.com/"; } else{ alert("Thanks for sticking around!") } } </script> </head> <body> <form> <input type="button" onclick="confirmation()" value="Leave This Page"> </form> </body> </html>
CHAPTER - 13
- Get link
- X
- Other Apps
Object In Javascript Objects are also a kind of variable but the difference between a normal variable and an object is that an object can hold many values while a variable can hold only one value. The object value is always in the key-value pair. Example: var languages = { ‘Graphic’:’Photoshop’, ‘Coding’:’javascript’, ‘desktopApplication’:’Visual Basic’ }; Now if you want to display the value of any object element, you can do this. console.log(languages.Graphic); Result: Photoshop