Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Friday, 5 October 2018

Javascript - refresh parent window when closing child window

var win = window.open("URL", '_blank');
var timer = setInterval(function () {
if (win.closed) {
 clearInterval(timer);
window.location.reload();
// Refresh the parent page
 }
 }, 1000);
 }

Wednesday, 22 August 2018

JS cookie handling

<script type='text/javascript'>
function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return false;
}
function eraseCookie(name) { 
    document.cookie = name+'=; Max-Age=-99999999;'; 
}
setCookie('TEST','Goal',7);
alert(getCookie('TEST'));
eraseCookie('TEST');
alert(getCookie('TEST'));
</script>

Friday, 18 May 2018

Difference between two dates in Javascript with demo


Enter values in mm/dd/yy hh:mm:ss format
First date and time
Second date and time
Result
In (decimal) hours
In (decimal) minutes
In seconds

Code:
<script type='text/javascript'>
function p (i){
return Math.floor(i / 10) + "" + i % 10;
}
function trunc (i){
var j = Math.round(i * 100);
return Math.floor(j / 100) + (j % 100 > 0 ? "." + p(j % 100) : "");
}
function calculate (form){
var date1 = new Date(form.date1.value);
alert(form.date1.value+"\n\n"+date1)
var date2 = new Date(form.date2.value);
var sec = date2.getTime() - date1.getTime();
if (isNaN(sec)){
alert("Input data is incorrect!");
return;
}
if (sec < 0){
alert("The second date ocurred earlier than the first one!");
return;
}
var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;
form.result_h.value = trunc(sec / hour);
form.result_m.value = trunc(sec / minute);
form.result_s.value = trunc(sec / second);
var days = Math.floor(sec / day);
sec -= days * day;
var hours = Math.floor(sec / hour);
sec -= hours * hour;
var minutes = Math.floor(sec / minute);
sec -= minutes * minute;
var seconds = Math.floor(sec / second);
form.result.value = days + " day" + (days != 1 ? "s" : "") + ", " + hours + " hour" + (hours != 1 ? "s" : "") + ", " + minutes + " minute" + (minutes != 1 ? "s" : "") + ", " + seconds + " second" + (seconds != 1 ? "s" : "");
}
</script>
<form id="form">
<table cellpadding="3">
<tr>
<td colspan="2" align="center">Enter values in <font color="#800000">mm/dd/yy hh:mm:ss</font> format</td>
</tr>
<tr>
<td>First date and time</td>
<td><input type="text" name="date1" /></td>
</tr>
<tr>
<td>Second date and time</td>
<td><input type="text" name="date2" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" name="submit" value="Calculate" onclick="calculate(this.form)" /></td>
</tr>
<tr>
<td>Result</td>
<td colspan="2"><input type="text" name="result" readonly="readonly" size="40" /></td>
</tr>
<tr>
<td>In (decimal) hours</td>
<td colspan="2"><input type="text" name="result_h" readonly="readonly" /></td>
</tr>
<tr>
<td>In (decimal) minutes</td>
<td colspan="2"><input type="text" name="result_m" readonly="readonly" /></td>
</tr>
<tr>
<td>In seconds</td>
<td colspan="2"><input type="text" name="result_s" readonly="readonly" /></td>
</tr>
</table>

Wednesday, 5 November 2014

How to disable Ctrl+V (Paste) , Ctrl+X (Cut) and Ctrl+C (Copy) with jQuery?

<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
 $(document).ready(function(){
      $('#read').bind("cut copy paste",function(e) {
   e.preventDefault();
   alert(" 'Ctrl+c & Ctrl+x & Ctrl+v has been disabled' ")
      });
   });
});
</script>
<input type="text" id="read" />

Friday, 10 October 2014

Autocomplete JQuery script full source code example

<script src="http://www.jqueryautocomplete.com/js/jquery/jquery-1.7.1.min.js"></script>
<script src="http://www.jqueryautocomplete.com/js/jquery/jquery.ui.core.min.js"></script>
<script src="http://www.jqueryautocomplete.com/js/jquery/jquery.ui.widget.min.js"></script>
<script src="http://www.jqueryautocomplete.com/js/jquery/jquery.ui.position.min.js"></script>
<script src="http://www.jqueryautocomplete.com/js/jquery/jquery.ui.autocomplete.min.js"></script>
<link rel="stylesheet" href="http://www.jqueryautocomplete.com/js/jquery/smoothness/jquery-ui-1.8.16.custom.css"/>
<script>var data = [
  {"label" : "Aragorn"},
  {"label" : "Arwen"},
  {"label" : "Bilbo Baggins"},
  {"label" : "Boromir"},
  {"label" : "Frodo Baggins"},
  {"label" : "Gandalf"},
  {"label" : "Gimli"},
  {"label" : "Gollum"},
  {"label" : "Legolas"},
  {"label" : "Meriadoc Merry Brandybuck"},
  {"label" : "Peregrin Pippin Took"},
  {"label" : "Samwise Gamgee"}
  ];
</script>
<script>
$(function() {

 $( "#search" ).autocomplete(
 {
   source:data
 })
});
</script>

<input type="text" id="search" />

Saturday, 4 October 2014

Friday, 26 September 2014

Push method for javascript array

JavaScript Array pop() Method
var actors = ["Rajini", "Kamal", "Ajith", "Vijay"];
actors.push("Surya");
The result of fruits will be:
Rajini,Kamal,Ajith,Vijay,Surya

Wednesday, 17 September 2014

How to get checked values in checkbox in dynamically by using jquery and javascript?

var chkArray = [];
$(".chk:checked").each(function() {
 chkArray.push($(this).val());
});
var selected;
selected = chkArray.join(',') + ",";
if(selected.length > 1){
 alert("You have selected " + selected); 
}else{
 alert("Please at least one of the checkbox"); 
}

Friday, 12 September 2014

Tuesday, 19 August 2014

Input field JQuery float validation, decimal validation

$('#price').keyup(function(e){
      var entered_value = $(this).val();
  var regexPattern = /^\d{0,8}(\.\d{1,2})?$/;         
      //Allow only Number as well 0nly 2 digit after dot(.)
      if(regexPattern.test(entered_value)) {
        alert("false") ;  
      } else {
         alert("true");
      }
});

Tuesday, 12 August 2014

Datatable jQuery Plugin set order example

$(document).ready(function() {
    $('#example').dataTable( {
        "order": [[ 3, "desc" ]]
    } );
} );
Plugin files:
//code.jquery.com/jquery-1.11.1.min.js
//cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js

Saturday, 28 June 2014

JS get random Numbers

function getRandom(length) {
 var rand = Math.floor(Math.pow(10, length-1) + Math.random() * 9 * Math.pow(10, length-1));
 return rand;
}

Monday, 12 May 2014

Image uploading Instant preview




<img src="http://www.designofsignage.com/application/symbol/building/image/600x600/no-photo.jpg" width='200px' height='200px' id="preview" title="No Image"><br><br>
<input type="file"  name="postimage" id="postimage" onchange="readURL(this,200,200)" >
<script type='text/javascript' src='http://code.jquery.com/jquery-1.11.1.min.js'></script>
<script type='text/javascript'>
function readURL(input,w,h) {
 if (input.files && input.files[0]) {
  var reader = new FileReader();
  reader.onload = function (e) {
   $('#preview').attr('src', e.target.result);
   $("#preview").attr("width",w);
   $("#preview").attr("height",h);
  }
  reader.readAsDataURL(input.files[0]);
 }
}
</script>

Friday, 11 April 2014

Input field enter integer only


Here's an example that limits a form field to contain only numeric input (on the fly, of course!).
<script type="text/javascript">
function numbersonly(e){
var unicode=e.charCode? e.charCode : e.keyCode
if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
if (unicode<48||unicode>57) //if not a number
return false //disable key press
}
}
</script>

<form>
<input type="text" size=18 onkeypress="return numbersonly(event)">
</form>