Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Tuesday, 17 June 2014

Add Html5 Tags in Internet Explorer 8

Introduction:

Html5 is popular language but some of older browser such as IE8,IE7 and lower not supported Html5.

Html5 introduce some new elements such as header,section but older browser doesn't know about these tags then it can't generate them so you will find that in IE8 or lower your website will be without any styles.

Here is solution for add Html5 tags in IE8 and lower.

First we have to detect IE Version and then we have to create elements which is added by Html5.

Code:


var userAgent = navigator.userAgent.toLowerCase();
// Test if the browser is IE and check the version number is lower than 9
if (/msie/.test(userAgent) && 
    parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]) < 9) {
 
         document.createElement('header');
         document.createElement('section');
         document.createElement('article');
         document.createElement('aside');
         document.createElement('nav');
         document.createElement('footer');
}


Copy the following code at top of page.

<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->



Detect mobile device using JavaScript

Introduction:

Here i will explain how to detect mobile device using JavaScript

This is simplest way to detect mobile device.

Code:

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 // some code..
}


Friday, 25 April 2014

Remove HTML tags from string but keep content.

Introduction:

Here I will explain how to remove specific HTML tag from string but keep content which is inside tag.

In JQuery we can remove element by remove()  function but how would we remove specific HTML tag from string.

Just need to use unwrap() function of JQuery.

Code:
$('p').contents().unwrap();



Example:






Suppose we want to remove all p page from string.

var temp='<div>Some text</div><p style="color:red">More text<span>here</span></p><p>Even more</p>';

var StrTemp=$(temp).find('p').contents().unwrap();

alert(StrTemp);



This will alert "<div>Some text</div>More text<span>here</span>Even more"
unwrap functino remove all p tag from string.



Monday, 31 March 2014

JQuery Check element is block or hidden.

Introduction:

Here I will explain how to check any element like div,p,input is block or hidden.

In JQuery we can show/hide element by show(), hide() functions but how would we test element is visible or not so here is solution for check visibility of element.

Just need to check visible property.

Code:
$(element).is(":visible");


Example:





Suppose you want to check visibility of div element then just select div.

var b = $('div.test').is(":visible");;
alert(b);


This will alert true if div with class test is visible and return false if div with class test is hidden.



JavaScript get query string values

Introduction:

Here I will explain how to get Query String value from URL in JavaScript.

Code:
function getParameterByName(name) {
 name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
 var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
 results = regex.exec(location.search);
 return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}


Example:





Suppose you have three Query String in current URL a b and c and you need value of b so just need to pass b in this function.

var b = getParameterByName('b');
alert(b);

This will alert value of b.



Thursday, 12 September 2013

Create a Startwith Function in JavaScript.

Javascript does not have a StartsWith function so we have to create it manually we use String prototype for create StartsWith function.
if (typeof String.prototype.startsWith != 'function') {
  // see below for better implementation!
  String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
  };
}
Then you can use it directly on string values.
"Hello World!".startsWith("He"); // true

var data = "Hello world";
var input = 'He';
data.startsWith(input); // true




Thursday, 25 April 2013

Get Facebook Comments details by ajax request.

      var url = "http://getpaidtomeme.com/image.aspx?imagenum=15195";
      $.ajax({
            url: "https://graph.facebook.com/comments/?ids="+url+"",
            dataType: "jsonp",
            success: function (data) {
                 alert(JSON.stringify(data));
            },
            error: function (data) {
                consol.log(data);
            }
     });

Just change url here.

Live Demo



Get tweet and user detail from twitter API using JQuery

var username = "ankushjain99";
$.ajax({
            url: "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name="+username+"&count=20",
            dataType: "jsonp",
            success: function (data) {
                 alert(JSON.stringify(data));
            },
            error: function (data) {
                consol.log(data);
            }
});

Just Change username

Live Demo



Use Google Currency Converter API in JQuery

        function buildQuery(amount, from, to) {
            var str = "http://www.google.com/ig/calculator?hl=en&q=" + amount + "" + from + "%3D%3F" + to + "";
            return "select * from json where url ='" + str + "' ";
        }

        $.ajax({
            url: 'http://query.yahooapis.com/v1/public/yql',
            data: {
                q: buildQuery("100","KWD","INR"),
                format: "json"
            },
            dataType: "jsonp",
            success: function (data) {
                alert(data.query.results.json.lhs + "=" + data.query.results.json.rhs);
            },
            error: function (data) {
                consol.log(data);
            }
      });
Just change buildQuery("100","KWD","INR") parameters.

Live Demo



Restrict user to not paste in textbox by jquery

For disable paste option in textbox use this JQuery
<input type="text" id="txtBox" />
$(document(.ready(function(){ $('#txtBox').bind("paste", function(e) { e.preventDefault(); }); });

Live Demo

Tuesday, 23 April 2013

Change not found image by onerror event

We can change image Src when image is not found or image is not loaded by using onerror event of javascript. For Example

demo

img src="any url" onerror="changeimage(this)"

function changeimage(id) {
    id.src = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGhLzwUieFALJwbatbY8AsGwNKX2jG17_3f3ZTq1dwsGnx3-E0TLXpMaWriKxSXt2Q-JowqVyXAtYCXJ8H5elZJ4-P6eElixO1g8rOE10C3CqzSHufAQ5BbKKuCKjiqwvbiMEK-G7nJkBS/s1600/NoImage.jpg";
}
when image is not found then automatically this function change image src by noimage.jpg which is default image.

Saturday, 20 April 2013

Get number of likes on page from facebook by ajax request.

You can get number of likes of page from facebook by ajax request

Here is example
$.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "http://graph.facebook.com/https://www.facebook.com/Cyprusjetset",
            dataType: "json",
            success: function (data) {
                alert(data.likes);
            },
            error: function (result) {
                alert("Sorry no data found.");
            }
});




Jquery calender

Here is JQuery Full Calender example with events which is set by JSON :

Click Here to see

You can change Event name by changing events attribute title

You can also set start and end date see example

$(document).ready(function() {
 
  var date = new Date();
  var d = date.getDate();
  var m = date.getMonth();
  var y = date.getFullYear();
  
  $('#calendar').fullCalendar({
   theme: true,
   header: {
    left: 'prev,next today',
    center: 'title',
    right: 'month,agendaWeek,agendaDay'
   },
   editable: true,
   events: [
    {
     title: 'All Day Event',
     start: new Date(y, m, 1)
    },
    {
     title: 'Long Event',
     start: new Date(y, m, d-5),
     end: new Date(y, m, d-2)
    },
    {
     id: 999,
     title: 'Repeating Event',
     start: new Date(y, m, d-3, 16, 0),
     allDay: false
    },
    {
     id: 999,
     title: 'Repeating Event',
     start: new Date(y, m, d+4, 16, 0),
     allDay: false
    },
    {
     title: 'Meeting',
     start: new Date(y, m, d, 10, 30),
     allDay: false
    },
    {
     title: 'Lunch',
     start: new Date(y, m, d, 12, 0),
     end: new Date(y, m, d, 14, 0),
     allDay: false
    },
    {
     title: 'Birthday Party',
     start: new Date(y, m, d+1, 19, 0),
     end: new Date(y, m, d+1, 22, 30),
     allDay: false
    },
    {
     title: 'Click for Google',
     start: new Date(y, m, 28),
     end: new Date(y, m, 29),
     url: 'http://google.com/'
    }
   ]
  });
  
 });

List of simple Javascript Chart.

Here is List of simple Javascript Chart :-

Getting cross domain JSON Data by ajax request

Their is two way to get data from other domain.

1. By using YQL Yahoo Service. Here is example :-

function buildQuery(term) {
    return "select * from json where url = 'http://airportcode.riobard.com/search?fmt=JSON&q=" + term + "'";
}

$.ajax({
        url: 'http://query.yahooapis.com/v1/public/yql',
        data: {
            q: buildQuery(YourURL),
            format: "json"
        },
        dataType: "jsonp",
        success: function(data) {
            alert(JSON.stringify(data));
        },
        error: function (data) {
            consol.log(data);
        }
});


2. By using $.getJSON Here is example-


$.getJSON("http://kalpa.freeoda.com/news/news_feed.php?callback=?", null, function (data) {
            alert(JSON.stringify(data));
});