|
/*! Form Behaviors */ |
|
|
|
$(document).ready(function () { |
|
$('button[type="submit"]').on('click', function (e) { |
|
// prevent default action and bubbling |
|
e.preventDefault(); |
|
e.stopPropagation(); |
|
// regex: url must start with "http://" or "https://" and end with ".js" |
|
var filter = /https?:\/\/.+\.js/g; |
|
// input field variable |
|
var file = $('input[type="url"]').val(); |
|
// default error status |
|
var error = false; |
|
// if url is invalid... * |
|
if (!filter.test(file)) { |
|
// * show invalid message and hide others |
|
$('.invalid').show().delay(2000).fadeOut(300); |
|
$('.success, .error, .loading').hide(); |
|
// * focus on url field |
|
$('input[type="url"]').focus(); |
|
// * set error status |
|
error = true; |
|
// if url is valid... * |
|
} else { |
|
// * show loading message |
|
$('.loading').show(); |
|
// * cache the script (optional) |
|
$.ajaxSetup({ cache: true }); |
|
// * ajax form submission handler |
|
$.getScript(file).done(function (script, textStatus) { |
|
// show success message and hide others |
|
$('.invalid, .error, .loading').hide(); |
|
$('.success').show().delay(2000).fadeOut(300); |
|
// clear contents of url field |
|
$('input[type="url"]').val(''); |
|
// log to console |
|
console.log(file + ' | ' + textStatus); |
|
// * for cross-domain requests, the '.fail()' and '.always()' methods will only fire if you're using jQuery 2.x or higher |
|
}).fail(function (jqxhr, settings, exception) { |
|
// show error message and hide others |
|
$('.invalid, .success, .loading').hide(); |
|
$('.error').show().delay(2000).fadeOut(300); |
|
// log to console |
|
console.log(exception); |
|
}); |
|
} |
|
}); |
|
}); |