Tutorialspoint

JavaScript Example on href vs onClick

<html>
 <head>
 <title>JavaScript href vs onClick()</title>
 <script>
 function myFunc() {
 var v = 0;
 for (var j=0; j<1000; j++) {
 v+=j;
 }
 alert(v);
 }
 </script>
 <a href="javascript:myFunc()">href</a>
 <a href="#" onclick="javascript:myFunc()">onclick</a>
 </head>
 <body>
 </body>
</html>

JavaScript Date Object Sample Program

<html>

 <head>
 <title>JavaScript setSeconds Method</title>
 </head>

 <body>

 <script>
 var dt = new Date("December 30, 2017 11:20:25");
 dt.setSeconds( dt.getSeconds() + 10 );
 document.write( dt );
 </script>

 </body>
</html>

Program for adding 2 hours to Javascript

<html>

 <head>
 <title>JavaScript setHours Method</title>
 </head>

 <body>

 <script>
 var dt = new Date();
 dt.setHours( dt.getHours() + 2 );
 document.write( dt );
 </script>

 </body>
</html>

Format Date Javascript Program

<html>

 <head>
 <title>JavaScript Dates</title>
 </head>

 <body>

 <script>
 var date, res;
 date = new Date();
 res = date.toISOString();

 document.write(res);
 </script>

 </body>
</html>

JavaScript Date Program

<html>

 <head>
 <title>JavaScript setMinutes Method</title>
 </head>

 <body>

 <script>
 var dt = new Date();
 dt.setMinutes( dt.getMinutes() + 30 );
 document.write( dt );
 </script>

 </body>
</html>

Program to Subtract days from a date in Javascript

<html>

 <head>
 <title>JavaScript setDate Method</title>
 </head>

 <body>

 <script>
 var dt = new Date("December 30, 2017 11:20:25");
 dt.setDate( dt.getDate() - 10 );
 document.write( dt );
 </script>

 </body>
</html>

Comparing Two Dates using Javascript Program

<!DOCTYPE html>
<html>
<body>
<script>
 var date1, date2;
 date1 = new Date();
 document.write(date1);

 date2 = new Date( "Dec 15, 2014 21:20:15" );
 document.write("<br>"+date2);
 if (date1 > date2) {
 document.write("<br>Date1 is the recent date.");
 } else {
 document.write("<br>Date 2 is the recent date.");
 }
</script>
</body>
</html>

Delaying a Javascript function using Javascript

<!DOCTYPE html>
<html>
<body>
<button onclick="timeFunction()">Submit</button>
<script>
function timeFunction() {
 setTimeout(function(){ alert("After 5 seconds!"); }, 5000);
}
</script>
<p>Click the above button and wait for 5 seconds.</p>
</body>
</html>

Calling jQuery function with Javascript function

<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
 $.myjQuery = function() {
 alert("jQuery");
 };
 $(document).ready(function() {
 alert("Welcome!");
 });
 function display() {
 $.myjQuery();
};
</script>
<input type="button" value="submit" onclick=" display();">
</body>
</html>

JSON with Ajax Example

<html>
   <head>
      <meta content = "text/html; charset = ISO-8859-1" http-equiv = "content-type">
		
      <script type = "application/javascript">
         function loadJSON(){
            var data_file = "http://www.tutorialspoint.com/json/data.json";
            var http_request = new XMLHttpRequest();
            try{
               // Opera 8.0+, Firefox, Chrome, Safari
               http_request = new XMLHttpRequest();
            }catch (e){
               // Internet Explorer Browsers
               try{
                  http_request = new ActiveXObject("Msxml2.XMLHTTP");
					
               }catch (e) {
				
                  try{
                     http_request = new ActiveXObject("Microsoft.XMLHTTP");
                  }catch (e){
                     // Something went wrong
                     alert("Your browser broke!");
                     return false;
                  }
					
               }
            }
			
            http_request.onreadystatechange = function(){
			
               if (http_request.readyState == 4  ){
                  // Javascript function JSON.parse to parse JSON data
                  var jsonObj = JSON.parse(http_request.responseText);

                  // jsonObj variable now contains the data structure and can
                  // be accessed as jsonObj.name and jsonObj.country.
                  document.getElementById("Name").innerHTML = jsonObj.name;
                  document.getElementById("Country").innerHTML = jsonObj.country;
               }
            }
			
            http_request.open("GET", data_file, true);
            http_request.send();
         }
		
      </script>
	
      <title>tutorialspoint.com JSON</title>
   </head>
	
   <body>
      <h1>Cricketer Details</h1>
		
      <table class = "src">
         <tr><th>Name</th><th>Country</th></tr>
         <tr><td><div id = "Name">Sachin</div></td>
         <td><div id = "Country">India</div></td></tr>
      </table>

      <div class = "central">
         <button type = "button" onclick = "loadJSON()">Update Details </button>
      </div>
		
   </body>
		
</html>

1 2 3 4 5 6 7 ... 87 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.