Tag: jquery

Get query string values in JavaScript

Author: | Categories: Programming No Comments
Option 1 No need jQuery for that purpose. Just pure JavaScript: 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, " ")); } Usage: var prodId = getParameterByName('prodId'); Option 2 var urlParams; (window.onpopstate = function () […]

Detect a mobile device in jQuery

Author: | Categories: Programming 4 Comments
Below are few options to detect whether web request is from a Mobile device using Javascript. Option 1 if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) { // some code.. } Option 2 var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); }, iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera: function() { return […]

Detecting Mouse Movement with jQuery

Author: | Categories: Programming No Comments
Javascript / jQuery can help in capturing Mouse move, Scroll and Click events Option 1 – Capturing Mouse move along with Click and Scroll var timeout = null; $(document).on('mousemove click scroll', function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log('Mouse idle for 3 sec'); }, 3000); }); Option 2 – Capturing Mouse with Javascript (function() { […]

JQUERY AJAX different ways

Author: | Categories: Programming No Comments
Below are different ways to perform AJAX Get and Post requests through jQuery. GET request method 1 $.ajax({ url: url, data: data, success: success, dataType: dataType }); GET request method 2 $.get( "ajax/test.html", function( data ) { $( ".result" ).html( data ); alert( "Load was performed." ); }); GET request method 3 – jqXHR // […]

Waiting for AJAX response

Author: | Categories: Programming No Comments
Below code will fade out all elements in a web page till AJAX completion of request. $("body").ajaxSend(function() { $(this).append('<div id="loading">Data Loading</div>'); $('#tag').children().not('#loading').css({'opacity':0.22}); }); $("body").ajaxComplete(function() { $("div#loading").remove(); $(#tag).children().not('#loading').css({'opacity':1}); });

ASP.NET AJAX File Upload

Author: | Categories: Programming No Comments
jQuery can help in uploading a file on asp.net web page asynchronously. Add a generic handler in asp.net project like below. Upload Generic Handler – Code behind file (upload.ashx) public class upload : IHttpHandler { public void ProcessRequest(HttpContext context) { string fileName = HttpContext.Current.Request.QueryString["FileName"].ToString(); using (FileStream fs = File.Create("[Path]\\" + fileName)) { Byte[] buffer = […]

JQuery snippets

Author: | Categories: Programming No Comments
Adding text to a span using jquery $("#spanid").text("Your text here"); Adding html to a span using jquery $("#spanid").text("HTML here"); Append html to a span using jquery $("#spanid").append("HTML here"); Check/uncheck a checkbox input or radio button $("#x").prop("checked", true); $("#x").prop("checked", false); Check checkbox checked property $("#x").is(':checked') Handle change of checkbox $('#x').change(function() { if($(this).is(":checked")) { //'checked' event code […]

ASP.NET Send Canvas to Server as File Using Ajax

Author: | Categories: Programming No Comments
HTML 5 introduces canvas element as a resolution-dependent bitmap canvas which can be used for rendering graphs, game graphics, or other visual images on the fly. A canvas is a rectangle in your page where you can use JavaScript to draw anything you want. Below is the code save graphics drawn on Canvas to server […]

ASP.NET calling Webmethod using jQuery

Author: | Categories: Programming No Comments
To call Webmethod asynchronously in ASP.net through jQuery Javascript Code <script type=”text/javascript”> function Authenticate() { $.ajax({ type: "POST", url: "/login.aspx/Authenticate", data: "{ provide json data }", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { if (msg.d == '1') { alert(‘authenticated’); } else { alert("not authenticated"); } }, error: function() { alert("Error :("); } }); }; […]

Check / Uncheck checkbox and radio button with jquery

Author: | Categories: Programming No Comments
You can check or uncheck a checkbox element or a radio button using the .prop() method // Check #x $( "#x" ).prop( "checked", true ); // Uncheck #x $( "#x" ).prop( "checked", false );