Tag "JavaScript" (12)
«jQuery Trickshots» by tutorialzine.com ... Reveal Code
// Loop all .fetchSize links
$('a.fetchSize').each(function(){
// Issue an AJAX HEAD request for each one
var link = this;
$.ajax({
type: 'HEAD',
url: link.href,
complete: function(xhr){
var size = humanize(xhr.getResponseHeader('Content-Length'));
// Append the filesize to each
$(link).append(' (' + type + ')');
}
});
});
function humanize(size){
var units = ['bytes','KB','MB','GB','TB','PB'];
var ord = Math.floor( Math.log(size) / Math.log(1024) );
ord = Math.min( Math.max(0,ord), units.length-1);
var s = Math.round((size / Math.pow(1024,ord))*100)/100;
return s + ' ' + units[ord];
}
// html
// First Trickshot
// This Trickshot
// Ball.png
jQuery: Get data via Ajax
06.05.2013 03:14 TestUser jQuery
Get some data via Ajax, calling to a php-script and receiving a data. ... Reveal Code
$.ajax({
url: "ajax.php",
type: "POST",
data: "id=" + id_value + "&mode=" + mode_value,
cache: false,
success: function(data) {
$('#ajax_result').html($.trim(data))
}
});
Добавить текст при копировании контента ... Reveal Code
function addLink() {
var body_element = document.getElementsByTagName('body')[0];
var selection = window.getSelection();
// Вы можете изменить текст в этой строчке
var pagelink = " Источник: "+document.location.href+"
© snippets.pro
";
var copytext = selection + pagelink;
var newdiv = document.createElement('div');
newdiv.style.position = 'absolute';
newdiv.style.left = '-99999px';
body_element.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout( function() {
body_element.removeChild(newdiv);
}, 0);
}
document.oncopy = addLink;
jQuery: Keyboard shortcut
Using the keyboard shortcut to increase the value in the input field ... Reveal Code
var isShift = false;
$('input[type="text"]').keyup( function(e){
if (e.which == 16 ) isShift = false;
}).keydown( function(e) {
$(this).attr('autocomplete',"off");
// Shift key
if(e.which == 16) isShift = true;
var v = (isShift == true) ? 10 : 1;
// Arrow up code
if (e.keyCode == 38)
$(this).val( parseInt( $(this).val() ) + v );
// Arrow down code
if (e.keyCode == 40)
$(this).val( parseInt( $(this).val() ) - v );
});
JavaScript: Send a POST request
Send a POST request in the same way as it does form's submit. Was found here: http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit ... Reveal Code
jQuery: Scroll to top
Scroll the page to its top ... Reveal Code
$("a[href='#top']").click(function() {
$("html, body").animate({ scrollTop: 0 }, "fast");
return false;
});
Tracking the Facebook comments: creating and removing. You can use it to count the FB comments in the local database. ... Reveal Code
JavaScript: Email validation
Validation of the entered email using regex ... Reveal Code
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
jQuery: Fade in then fade out
Fade in the hidden element, wait a little bit, then fade out id. Simple but useful. ... Reveal Code
$("#error_box").fadeIn(500).delay(3000).fadeOut(500);