Skip to content

Instantly share code, notes, and snippets.

@jp26jp
jp26jp / .js
Created November 15, 2016 06:01
Find each ul and increase the data-index value
$('ul').find('ul').each(function(idx){
$(this).attr('data-index', idx);
});
@jp26jp
jp26jp / .js
Created November 16, 2016 06:40
Use getJSON with a preloader
$(".someSpinnerImage").show();
$.getJSON('file.php', function(json) {
$.each(json, function() {
// Retrieving data from json...
});
$(".someSpinnerImage").hide();
});
@jp26jp
jp26jp / .js
Created November 23, 2016 03:42
Select all text
$('pre code').on('mouseup', function() {
var sel, range;
var el = $(this)[0];
if (window.getSelection && document.createRange) { //Browser compatibility
sel = window.getSelection();
if(sel.toString() == ''){ //no text selection
window.setTimeout(function(){
range = document.createRange(); //range object
range.selectNodeContents(el); //sets Range
sel.removeAllRanges(); //remove all ranges from selection
@jp26jp
jp26jp / .htaccess
Last active January 12, 2017 16:21
Parse php in html with .htaccess
# Place this line of code anywhere in your .htaccess file
AddType application/x-httpd-php .html .htm
# This doesn't work for all servers and so if this doesn't
# work for yours, delete the code above and use the code
# below. If you use the code below, don't forget to remove
# the hash character '#'
# AddHandler application/x-httpd-php .html .htm
@jp26jp
jp26jp / .htaccess
Last active January 18, 2017 13:04
Remove User-Agent from Vary header with .htaccess
Header set Vary "Accept-Encoding"
@jp26jp
jp26jp / .htaccess
Created February 10, 2017 18:04
Force .html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ /$1.html [L,R=301]
@jp26jp
jp26jp / .js
Created March 1, 2017 11:55
Remove all line breaks with javascript
var str = "Hello,\nMy name is, Bob!\n";
str.replace(/(\r\n|\n|\r)/gm,"");
@jp26jp
jp26jp / .js
Created May 8, 2017 18:09
Scroll only in hovered div
$('#menu').on('DOMMouseScroll mousewheel', function(ev) {
var $this = $(this),
scrollTop = this.scrollTop,
scrollHeight = this.scrollHeight,
height = $this.height(),
delta = (ev.type == 'DOMMouseScroll' ?
ev.originalEvent.detail * -40 :
ev.originalEvent.wheelDelta),
up = delta > 0;
@jp26jp
jp26jp / .java
Last active September 16, 2017 21:55
/**
* We figured out part of the answer to our question.
* So now we'd just like to know if we're on the right track.
*/
public void insert(int index, E data)
{
ensureCapacity(indexCounter + 1);
for (int i = elementData.length; i > index; i--)
@jp26jp
jp26jp / smooth-scroll.js
Created December 4, 2017 19:31
Smooth scrolling
$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(function(event) {
// On-page links
if (
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
&&
location.hostname == this.hostname