-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.formatUrl-1.0.5.js
55 lines (51 loc) · 1.57 KB
/
jquery.formatUrl-1.0.5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* jQuery Format URL
* Copyright (c) 2013 Rob Lee <http://www.spartansystems.co>
* licensed under the LGPL Version 3 license.
* http://www.gnu.org/licenses/lgpl.html
*/
;(function ($) {
$.fn.formatUrl = function(options) {
// Set default settings.
var settings = $.extend({
protocol: 'http://',
alwaysShow: false
}, options);
// On Focus we want to check whether the protocol exists and act on it.
$(this).live('focus', function () {
if ($(this).val().indexOf(settings.protocol) != 0) {
var val = $(this).val();
$(this).val(settings.protocol + val);
}
});
// On Blur we want to check whether the protocol exists and act on it.
$(this).live('blur', function () {
// If the protocol isn't there.
if ($(this).val().indexOf(settings.protocol) != 0) {
if ($(this).val() == '') {
// If alwaysShow is false, remove the protocol.
if (settings.alwaysShow == false) {
$(this).val('');
}
else {
// If alwaysShow is true, always show the protocol.
$(this).val(settings.protocol);
}
}
else {
// If the value is not empty.
var val = $(this).val();
$(this).val(settings.protocol + val);
}
}
else {
// If just the protocol exists, act on it based on alwaysShow.
if ($(this).val() == settings.protocol && settings.alwaysShow == false) {
$(this).val('');
}
}
});
// Return for chaining.
return this;
};
})(jQuery);