Category: JAVASCRIPT

How to reload page every 5 second or 10 second like this?

For auto reload your page using javascript or simple html and also clear cache when reload your page then you can do it easily using javascript setInterval function. Here is simple code which help you to auto reload your current page


$(document).ready(function() {
  setInterval(function() {
    cache_clear()
  }, 5000);
});

function cache_clear() {
  window.location.reload(true);
  // window.location.reload(); use this if you do not remove cache
}

This is html section

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<p>Auto reload page and clear cache</p>

just copy and paste this code your editor then run this in your browser , you can see that your page reload after 5 seconds , you can change value , 5 seconds = 5000ms.

you can also do this using html meta tag . this is also simple hmtl code which help you

<meta http-equiv="Refresh" content="5">
Comment

Input tab index auto jump using jQuery.

In html form or other some time need tab auto jump when max length is covered. We can do it easily javascript code.

First create simple html code

<input size="2" tabindex="1" id="one"
maxlength="2" onkeyup="toUnicode(this)" />

<input size="2" tabindex="2" id="two"
maxlength="2" onkeyup="toUnicode(this)" />

<input size="2" tabindex="3" id="three"
maxlength="2" onkeyup="toUnicode(this)" />

then create javascript code. this is javascript code.

function toUnicode(elmnt)
{
  var next;
  if (elmnt.value.length==elmnt.maxLength)
  {
    next=elmnt.tabIndex + 1;
    //look for the fields with the next tabIndex
    var f = elmnt.form;
    for (var i = 0; i < f.elements.length; i++)
    {
      if (next<=f.elements[i].tabIndex)
      {
        f.elements[i].focus();
        break;
      }
    }
  }
}

then browse it and press onkeyup then automatic go to next input field.

Comment

How can I load jQuery if it is not loaded?

If you need to check that a specific jQuery load or not load if not loaded then load it or not load . You can do this using jquery . This is simple code.

<script type="text/javascript">
 var baseurl="shafiqul.wordpress.com";
 var doc=document.getElementsByTagName("head").item(0);
 var jQueryScriptOutputted = false;
 var jQueryScriptOutputtedplaceholder = false;
 window.onload=function()
 {
 initJQuery();
 }
 function initJQuery() {
 if (typeof(jQuery) == "undefined") {
 if (! jQueryScriptOutputted) {
 jQueryScriptOutputted = true;
 includeJs("/js/jquery-1.4.4.min.js", true);
| }
 }
 if (! jQueryScriptOutputtedplaceholder) {
 jQueryScriptOutputtedplaceholder = true;
 includeJs("/scripts/html5_placeholder.js", true);
 }
 setTimeout("initJQuery()", 100);
 }
 function includeJs(fileName, jQ, head){
 var js=document.createElement("script");
 js.setAttribute("language", "javascript");
 js.setAttribute("type", "text/javascript");
 js.setAttribute("src", baseurl+fileName);
 if(jQ===true){
 doc.insertBefore(js, doc.firstChild);
 return false;
 }
 if(head===true){
 doc.appendChild(js);
 return false;
 }
 document.getElementsByTagName("body").item(0).appendChild(js);
 return false;
 }
 </script>

This code first check jquery already or not, if not loaded then load it. If you face any problem then please inform me.

Comment

Show Content if class attribute exists in page.

If you want to show content when class exit in this page then you can use this code .It will give alert ok when class attribute exit in this page , you can change also class to id if you want.

<body>
<div class="home-container">
    <div class="home">

        Content

    </div>
    <span style="cursor: pointer; background: #0f192a;color: #00BD9B" onclick="test()">Click</span>
</div>
</body>

then this is javascript code which give alert if class attribute exit in this page

function test() {
        if (document.querySelector('.home') !== null) {
            alert('ok');
        } else {
            alert('not');
        }
    }

In this code home attribute exit so it will give alert ok.
If you find any problem then please inform me.

Comment

how to remove id and add new id using jQuery?

For remove exiting id from attribute and add new id , in this case you can use jQuery code which very simple to do this. this is demo code

<script>
$(document).ready(function(){
$('.test').removeAttr('id');
$('.test').attr('id','test1');
});
</script>

and this is html code section.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
<p id="test" class="test">Add new id</p>
</body>

this is code i add ID test and add class also test so i want to remove id and add new ID test1 . so please check this code which help you.
If you face any problem then please inform me.

Comment

make two dropdown values from an array using JavaScript and jQuery.

If you want to make two select dropdown using javascript array then you can use this code which help you.

This is html code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selectNumber">
	<option>Choose a number1</option>
</select>
<select id="selectNumber2">
	<option>Choose a number2</option>
</select>

this is jQuery code

<script> 
var options = ["1", "2", "3", "4", "5"];

    $.each(options, function(key, value) {
        $('#selectNumber,#selectNumber2').append($("<option/>", {
            value: key,
            text: value
        }));
    });

</script>

There is other option

<script>
        var select = document.getElementById("selectNumber");
        var select2 = document.getElementById("selectNumber2");
        var options = ["1", "2", "3", "4", "5"];
        for (var i = 0; i < options.length; i++) {
            var opt = options[i];
            var el = document.createElement("option");
            el.textContent = opt;
            el.value = opt;
            select.appendChild(el);
            select2.appendChild(el.cloneNode(true));
        }
    });
</script>

This is other option if you want separate the option code

<script>
    var select = document.getElementById("selectNumber");
    var select2 = document.getElementById("selectNumber2");
    var options = ["1", "2", "3", "4", "5"];
    for (var i = 0; i < options.length; i++) {
        var opt = options[i];

        var el = document.createElement("option");
        el.textContent = opt;
        el.value = opt;
        select.appendChild(el);

        var e2 = document.createElement("option");
        e2.textContent = opt;
        e2.value = opt;
        select2.appendChild(e2);
    }
</script>

If you find any problem or face any problem then please inform me.

Comment

How to get the ‘height’ and ‘width’ of the screen using jquery?

If you want to show the screen height and width then you can use jQuery. You can aslo get giv height , width using this same process. This is simple code.

var height = $(window).height(); // for height of the browser view
var height = $(document).height(); // for height of HTML document
var width = $(window).width(); // for width of browser view
var width = $(document).width(); // for width of HTML document

If you want to know the screen width, height then this code help you

var height = screen.height;
var width = screen.width;

If you face any problem then please inform me. I will try to help you.

Comment

how to validation Email JavaScript regular expression ?

In your html form most of the time need to check email validation. In this example you will learn simple email validation. First take a text input in html and a button input like this. this is demo code which validate email

 
<input type='text' id='txtEmail'/>
<input type='submit' name='submit' onclick='checkEmail();'/>
<script>
    function checkEmail() {
        var email = document.getElementById('txtEmail');
        var filter = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if (!filter.test(email.value)) {
            alert('Please provide a valid email address');
            email.focus;
            return false;
        }
    }
</script>

this is another regular expression which use for email validation

 
<input type='text' id='txtEmail'/>
<input type='submit' name='submit' onclick='checkEmail();'/>
<script>
    function checkEmail() {

        var email = document.getElementById('txtEmail');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        if (!filter.test(email.value)) {
            alert('Please provide a valid email address');
            email.focus;
            return false;
        }
    }
</script>

hope it will help you, if you need any help then please inform me, i will try to help you

Comment

Get all href value using jquery and replace it.

If you want to get all link using jQuery then you can use this code easily. Here is code which get all link using each function then check , if value is something and you want to change it.

<script type="text/javascript">
$('a').each(function() {
var value = $(this).attr('href');
if(value=='http://google.com')
{
$(this).attr('href', 'http://youtube.com');
}
});​
</script>

if you face any problem then please inform me.

Comment