JavaScript CodePaste.NET - paste and link .NET code
 
 
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Syntax:
Title:
") .css({ fontFamily: jContent.css("font-family"), minHeight: "18px" }); if (def.value) jEdit.val(def.value); else jEdit.val(def.editMode == "text" ? jContent.text() : jContext.html()); if (def.editClass) jEdit.addClass(def.editClass); else jEdit.width(jContent.width() - 10) .height(jContent.height()); jEdit.focus() .hide().fadeIn("slow") .data("editing", jContent.get(0)) .insertBefore(jContent) .keypress(function(e) { if (e.keyCode == 27) cleanupEditor(); }); jContent .data("editing", true) .hide(); jContent.data("cleanupEditor", function() { jEdit.remove(); jButton.remove(); jContent .data("editing", false) .data("cleanupEditor", null) .fadeIn("slow"); }); jButton.click(function(e) { var pass = { text: jEdit.val(), cleanup: jContent.data("cleanupEditor"), button: jButton, edit: jEdit, content: jContent }; if (def.saveHandler.call(jEdit.get(0), pass)) cleanupEditor(); }); jEdit .after(jButton) .css("margin", 2); return this; }); return this; } $.maxZIndex = $.fn.maxZIndex = function(opt) { /// /// Returns the max zOrder in the document (no parameter) /// Sets max zOrder by passing a non-zero number /// which gets added to the highest zOrder. /// /// /// inc: increment value, /// group: selector for zIndex elements to find max for /// /// var def = { inc: 10, group: "*" }; $.extend(def, opt); var zmax = 0; $(def.group).each(function() { var cur = parseInt($(this).css('z-index')); zmax = cur > zmax ? cur : zmax; }); if (!this.jquery) return zmax; return this.each(function() { zmax += def.inc; $(this).css("z-index", zmax); }); } /// /// /// var tmpl = $("#itemtemplate").html(); /// var data = { content: "This is some textual content", /// names: ["rick", "markus"] /// }; /// $("#divResult").html(parseTemplate(tmpl, data)); /// /// based on John Resig's Micro Templating engine var _tmplCache = {} this.parseTemplate = function(str, data) { /// /// Client side template parser that uses <#= #> and <# code #> expressions. /// and # # code blocks for template expansion. /// /// The text of the template to expand /// /// Any data that is to be merged. Pass an object and /// that object's properties are visible as variables. /// /// var err = ""; try { var func = _tmplCache[str]; if (!func) { var strFunc = "var p=[];with(obj){p.push('" + str.replace(/[\r\t\n]/g, " ") .replace(/'(?=[^#]*#>)/g, "\t") .split("'").join("\\'") .split("\t").join("'") .replace(/<#=(.+?)#>/g, "',$1,'") .split("<#").join("');") .split("#>").join("p.push('") + "');}return p.join('');"; func = new Function("obj", strFunc); _tmplCache[str] = func; } return func(data); } catch (e) { err = e.message; } return "< # ERROR: " + err.htmlEncode() + " # >"; } function $$(id, context) { /// /// Searches for an ID based on ASP.NET naming container syntax. /// First search by ID as is, then uses attribute based lookup. /// Works only on single elements - not list items in enumerated /// containers. /// /// Element ID to look up /// /// /// var el = $("#" + id, context); if (el.length < 1) el = $("[id$=_" + id + "],[id*=[" + id + "_]", context); return el; } String.prototype.htmlEncode = function() { var div = document.createElement('div'); if (typeof (div.textContent) == 'string') div.textContent = this.toString(); else div.innerText = this.toString(); return div.innerHTML; } String.prototype.trimEnd = function(c) { if (c) return this.replace(new RegExp(c.escapeRegExp() + "*$"), ''); return this.replace(/\s+$/, ''); } String.prototype.trimStart = function(c) { if (c) return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), ''); return this.replace(/^\s+/, ''); } String.repeat = function(chr, count) { var str = ""; for (var x = 0; x < count; x++) { str += chr }; return str; } String.prototype.padL = function(width, pad) { if (!width || width < 1) return this; if (!pad) pad = " "; var length = width - this.length if (length < 1) return this.substr(0, width); return (String.repeat(pad, length) + this).substr(0, width); } String.prototype.padR = function(width, pad) { if (!width || width < 1) return this; if (!pad) pad = " "; var length = width - this.length if (length < 1) this.substr(0, width); return (this + String.repeat(pad, length)).substr(0, width); } String.startsWith = function(str) { if (!str) return false; return this.substr(0, str.length) == str; } String.prototype.escapeRegExp = function() { return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0"); }; String.format = function(frmt, args) { for (var x = 0; x < arguments.length; x++) { frmt = frmt.replace(new RegExp("\\{" + x.toString() + "\\}", "g"), arguments[x + 1]); } return frmt; } String.prototype.format = function() { var a = [this]; $.merge(a, arguments); return String.format.apply(this, a); } String.prototype.isNumber = function() { if (this.length == 0) return false; if ("0123456789".indexOf(this.charAt(0)) > -1) return true; return false; } var _monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; Date.prototype.formatDate = function(format) { var date = this; if (!format) format = "MM/dd/yyyy"; var month = date.getMonth(); var year = date.getFullYear(); if (format.indexOf("yyyy") > -1) format = format.replace("yyyy", year.toString()); else if (format.indexOf("yy") > -1) format = format.replace("yy", year.toString().substr(2, 2)); format = format.replace("dd", date.getDate().toString().padL(2, "0")); var hours = date.getHours(); if (format.indexOf("t") > -1) { if (hours > 11) format = format.replace("t", "pm") else format = format.replace("t", "am") } if (format.indexOf("HH") > -1) format = format.replace("HH", hours.toString().padL(2, "0")); if (format.indexOf("hh") > -1) { if (hours > 12) hours -= 12; if (hours == 0) hours = 12; format = format.replace("hh", hours.toString().padL(2, "0")); } if (format.indexOf("mm") > -1) format = format.replace("mm", date.getMinutes().toString().padL(2, "0")); if (format.indexOf("ss") > -1) format = format.replace("ss", date.getSeconds().toString().padL(2, "0")); if (format.indexOf("MMMM") > -1) format = format.replace("MMMM", _monthNames[month]); else if (format.indexOf("MMM") > -1) format = format.replace("MMM", _monthNames[month].substr(0, 3)); else format = format.replace("MM", (month + 1).toString().padL(2, "0")); return format; } Number.prototype.formatNumber = function(format, option) { var num = this; var fmt = Number.getNumberFormat(); if (format == "c") { num = Math.round(num * 100) / 100; option = option || "$"; num = num.toLocaleString(); var s = num.split("."); var p = s.length > 1 ? s[1] : ''; return option + s[0] + fmt.d + p.padR(2, '0'); } if (format.charAt(0) == "n") { if (format.length == 1) return num.toLocaleString() var dec = format.substr(1); dec = parseInt(dec); if (typeof (dec) != "number") return num.toLocaleString(); num = num.toFixed(dec); var x = num.split(fmt.d); var x1 = x[0]; var x2 = x.length > 1 ? fmt.d + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) x1 = x1.replace(rgx, '$1' + fmt.c + '$2'); return x1 + x2 } if (format.charAt(0) == "f") { if (format.length == 1) return num.toString(); var dc = format.substr(1); dc = parseFloat(dec); if (typeof (dec) != "number") return num.toString(); return num.toFixed(dec); } return num.toString(); } Number.getNumberFormat = function(cur) { var t = 1000.1.toLocaleString(); var r = {}; r.d = t.charAt(5); if (r.d.isNumber()) r.d = t.charAt(4); r.c = t.charAt(1); if (r.c.isNumber()) r.c = ","; r.s = cur || "$"; return r; } registerNamespace = function(ns) { var pts = ns.split('.'); var stk = window; var nsp = ""; for (var i = 0; i < pts.length; i++) { var pt = pts[i]; if (stk[pt]) stk = stk[pt]; else stk = stk[pt] = {}; } } getUrlEncodedKey = function(key, query) { if (!query) query = window.location.search; var re = new RegExp("[?|&]" + key + "=(.*?)&"); var matches = re.exec(query + "&"); if (!matches || matches.length < 2) return ""; return decodeURIComponent(matches[1].replace("+", " ")); } setUrlEncodedKey = function(key, value, query) { query = query || window.location.search; var q = query + "&"; var re = new RegExp("[?|&]" + key + "=.*?&"); if (!re.test(q)) q += key + "=" + encodeURI(value); else q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&"); q = q.trimStart("&").trimEnd("&"); return q.charAt(0) == "?" ? q : q = "?" + q; } $.fn.serializeNoViewState = function() { return this.find("input,textarea,select,hidden").not("#__VIEWSTATE,#__EVENTVALIDATION").serialize(); } $.fn.makeAbsolute = function(rebase) { /// /// Makes an element absolute /// /// forces element onto the body tag. Note: might effect rendering or references /// /// return this.each(function() { var el = $(this); var pos = el.position(); el.css({ position: "absolute", marginLeft: 0, marginTop: 0, top: pos.top, left: pos.left }); if (rebase) el.remove().appendTo("body"); }); } if (!this.assert) { this.assert = function(cond, msg) { if (cond) return; if (!msg) msg = ""; alert("Assert failed\r\n" + (msg ? msg : "") + "\r\n" + (arguments.callee.caller ? "in " + arguments.callee.caller.toString() : "")); } } var _ud = "undefined"; /* http://www.JSON.org/json2.js 2009-04-16 Public Domain. */ if (!this.JSON) { this.JSON = {}; } (function() { function f(n) { return n < 10 ? '0' + n : n; } if (typeof Date.prototype.toJSON !== 'function') { Date.prototype.toJSON = function(key) { return isFinite(this.valueOf()) ? this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z' : null; }; String.prototype.toJSON = Number.prototype.toJSON = Boolean.prototype.toJSON = function(key) { return this.valueOf(); }; } var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, gap, indent, meta = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' }, rep; function quote(string) { escapable.lastIndex = 0; return escapable.test(string) ? '"' + string.replace(escapable, function(a) { var c = meta[a]; return typeof c === 'string' ? c : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); }) + '"' : '"' + string + '"'; } function str(key, holder) { var i, k, v, length, mind = gap, partial, value = holder[key]; if (value && typeof value === 'object' && typeof value.toJSON === 'function') { value = value.toJSON(key); } if (typeof rep === 'function') { value = rep.call(holder, key, value); } switch (typeof value) { case 'string': return quote(value); case 'number': return isFinite(value) ? String(value) : 'null'; case 'boolean': case 'null': return String(value); case 'object': if (!value) { return 'null'; } gap += indent; partial = []; if (Object.prototype.toString.apply(value) === '[object Array]') { length = value.length; for (i = 0; i < length; i += 1) { partial[i] = str(i, value) || 'null'; } v = partial.length === 0 ? '[]' : gap ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : '[' + partial.join(',') + ']'; gap = mind; return v; } if (rep && typeof rep === 'object') { length = rep.length; for (i = 0; i < length; i += 1) { k = rep[i]; if (typeof k === 'string') { v = str(k, value); if (v) { partial.push(quote(k) + (gap ? ': ' : ':') + v); } } } } else { for (k in value) { if (Object.hasOwnProperty.call(value, k)) { v = str(k, value); if (v) { partial.push(quote(k) + (gap ? ': ' : ':') + v); } } } } v = partial.length === 0 ? '{}' : gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : '{' + partial.join(',') + '}'; gap = mind; return v; } } if (typeof JSON.stringify !== 'function') { JSON.stringify = function(value, replacer, space) { var i; gap = ''; indent = ''; if (typeof space === 'number') { for (i = 0; i < space; i += 1) { indent += ' '; } } else if (typeof space === 'string') { indent = space; } rep = replacer; if (replacer && typeof replacer !== 'function' && (typeof replacer !== 'object' || typeof replacer.length !== 'number')) { throw new Error('JSON.stringify'); } return str('', { '': value }); }; } if (typeof JSON.parse !== 'function') { JSON.parse = function(text, reviver) { var j; function walk(holder, key) { var k, v, value = holder[key]; if (value && typeof value === 'object') { for (k in value) { if (Object.hasOwnProperty.call(value, k)) { v = walk(value, k); if (v !== undefined) { value[k] = v; } else { delete value[k]; } } } } return reviver.call(holder, key, value); } cx.lastIndex = 0; if (cx.test(text)) { text = text.replace(cx, function(a) { return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); }); } if (/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { j = eval('(' + text + ')'); return typeof reviver === 'function' ? walk({ '': j }, '') : j; } throw new SyntaxError('JSON.parse'); }; } } ()); if (this.JSON && !this.JSON.parseWithDate) { var reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/; var reMsAjax = /^\/Date\((d|-|.*)\)[\/|\\]$/; JSON.parseWithDate = function(json) { /// /// parses a JSON string and turns ISO or MSAJAX date strings /// into native JS date objects /// /// json with dates to parse /// /// try { var res = JSON.parse(json, function(key, value) { if (typeof value === 'string') { var a = reISO.exec(value); if (a) return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])); a = reMsAjax.exec(value); if (a) { var b = a[1].split(/[-+,.]/); return new Date(b[0] ? +b[0] : 0 - +b[1]); } } return value; }); return res; } catch (e) { // orignal error thrown has no error message so rethrow with message throw new Error("JSON content could not be parsed"); return null; } }; JSON.stringifyWcf = function(json) { /// /// Wcf specific stringify that encodes dates in the /// a WCF compatible format ("/Date(9991231231)/") /// Note: this format works ONLY with WCF. /// ASMX can use ISO dates as of .NET 3.5 SP1 /// /// property name /// value of the property return JSON.stringify(json, function(key, value) { if (typeof value == "string") { var a = reISO.exec(value); if (a) { var val = '/Date(' + new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])).getTime() + ')/'; this[key] = val; return val; } } return value; }) }; JSON.dateStringToDate = function(dtString) { /// /// Converts a JSON ISO or MSAJAX string into a date object /// /// Date String /// var a = reISO.exec(dtString); if (a) return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])); a = reMsAjax.exec(dtString); if (a) { var b = a[1].split(/[-,.]/); return new Date(+b[0]); } return null; }; } //})(jQuery);
(left margins are automatically stripped from code)
Tags: (separate with commas)
Comment: (optional)
Your Name: (optional)
(JavaScript required for SPAM prevention)
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate