Thursday 12 September 2013

Create a endsWith Function in JavaScript.

Javascript does not have a endsWith function so we have to create it manually we use String prototype for create endsWith function.
if (typeof String.prototype.endsWith !== 'function') {
    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };
}
Then you can use it directly on string values.
var data = "Lorem ipsum dolor sit amet";
data.endsWith('amet'); //true



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

Wednesday 24 April 2013

Get wordpress blog Rss Feed in XML format

We can get wordpress blogs in XML format.

Just put /feed after url like

if your url is http://wordpress.org/showcase then

XML url is :- http://wordpress.org/showcase/feed/



Tuesday 23 April 2013

Change default port number Visual Studio

Step 1

Click on the web site or project node in Solution Explorer.

Step 2

Press F4, its showing properties tool window.

Step 3

Set "Use dynamic ports" to false

Step 4

Set "Port number" to whatever you need.



Force file download in PHP

This is code to force a browser to download file.
// Fetch the file info.
    $filePath = '/path/to/file/on/disk.jpg'; // File path here.

    if(file_exists($filePath)) {
        $fileName = basename($filePath); // change file name accroding your requirement here
        $fileSize = filesize($filePath);

        // Output headers.
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: ".$fileSize);
        header("Content-Disposition: attachment; filename=".$fileName);

        // Output file.
        readfile ($filePath);                   
        exit();
    }
    else {
        die('The provided file path is not valid.');
    }

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 = "http://3.bp.blogspot.com/-1jy-2M0NQ3Y/UXaFNs3oUZI/AAAAAAAAAaM/GtBpwYxkf0s/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.");
            }
});




Disable Visual Studio 2012 single click opens file in preview tab


You can disable visual studio 2012 single click opens file in preview tab solution Explorer

Step 1 

Click Tools --> Options

Step 2 

Click Environment --> Tabs and Windows

Step 3 :- 


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));
});