Tag Archives: jquery

Web

Check is jQuery loaded

jq
The simplest way to check jQuery library exists and loaded on page is next:

if (window.jQuery) {
  // your jQuery code here...
}

Thanx to http://stackoverflow.com/a/24284903

Web

Get cursor position in textarea or input with JQuery

cursor_pos

The next snippet is a JQuery function helps to obtain the posiion of cursor in any HTML input element:

    (function($, undefined) {
        $.fn.getCursorPosition = function() {
            var el = $(this).get(0);
            var pos = 0;
            if ('selectionStart' in el) {
                pos = el.selectionStart;
            } else if ('selection' in document) {
                el.focus();
                var Sel = document.selection.createRange();
                var SelLength = document.selection.createRange().text.length;
                Sel.moveStart('character', -el.value.length);
                pos = Sel.text.length - SelLength;
            }
            return pos;
        }
    })(jQuery);

And here usage example:



getCursorPosition

As the result we’ll see next:
cursor_pos_result

The post was created by originally
http://stackoverflow.com/questions/1891444/how-can-i-get-cursor-position-in-a-textarea/1909997#1909997

Web

Bind event to element in jQuery

ordjq
To bind any event to element (for ex. textarea) use next code:

$(document).ready(function() {
  var prevValue = $("#some_id").val();
  $("#some_id").bind('change click keyup', function(e){
    if($(this).val() != prevValue){
      alert(e.which);
    }
    prevValue = $(this).val();
  });
});
Web

JQuery ajax request

ajax
Next example demonstarates Login method:

var backEndpath = '/backend';

function unparam(p) {
  // http://stackoverflow.com/questions/1131630/javascript-jquery-param-inverse-function/4764403#4764403
  var ret = {}, seg = p.replace(/^.*\?/,'').split('&'), len = seg.length, i = 0, s;
  for (;i<len;i++) {
    if (!seg[i]) {
      continue;
    }
    s = seg[i].split('=');
    ret[s[0]] = s[1];
  }
  return ret;
}

function doLogin() {
  var data = unparam($.param( $('form input,textarea,select') ));
  data['operation_name'] = 'login';
  data['formatter'] = 'json';
  $.ajax({
      url: '/user',
      type: 'POST',
      data: data,
      success: function(data) {
        try {
          //alert(data);
          event = JSON.parse(data);
          if (event.is_error == 'true') {
            $('#login_result').html(event.message).show();
          }
          else {
            $(location).attr('href', backEndpath);
          }
        } catch(e) {
        alert(e);
        }
      },
      error: function(jqXHR, textStatus, errorThrown) {
        alert(textStatus + ': ' + errorThrown);
      }
  });
  return false;
}

Method unparam converts all form fields to array.

Web

Check browser version with jQuery

Some time we need to check a browser version on client and if it is not satisfies do something.
Very simple code in jQuery:

var browser = $.browser;
if (
      (browser.msie && browser.version < 7.0) || 
      (browser.mozilla && browser.version < 3.0) || 
      (browser.opera && browser.version < 8.0)
)
{
	$(location).attr('href', 'http://google.com');
}

So, we redirect to google.