Last active
October 19, 2016 19:27
-
-
Save willybahuaud/991a8044614054ae1849 to your computer and use it in GitHub Desktop.
Pour étendre les liens en javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($){ | |
function changeType( $elem, tag ) { | |
if ( tag == $elem.get(0).tagname ) { | |
return false; | |
} | |
var $newElem = $( '<' + tag + '>' ); | |
attributes = $elem.prop( 'attributes' ); | |
$.each(attributes, function() { | |
$newElem.attr( this.name, this.value ); | |
}); | |
$newElem.append( $elem.html() ); | |
$elem.replaceWith( $newElem ); | |
} | |
function applyExpandLink() { | |
var $parent, attributes, type; | |
$( 'a[data-expand-link]' ).each(function(){ | |
$parent = $(this).parents( '[data-expand-target]' ); | |
attributes = { | |
href: $(this).attr( 'href' ), | |
hreflang: $(this).prop( 'hreflang' ), | |
target: $(this).prop( 'target' ), | |
title: $(this).prop( 'title' ), | |
rel: $(this).prop( 'rel' ), | |
}; | |
type = $(this).attr( 'data-expand-type' ); | |
if ( 'undefined' == type || ! type ) { | |
type = 'div'; | |
} | |
$(this).removeAttr( 'data-expand-link \ | |
data-expand-type target \ | |
title rel href hreflang' ); | |
changeType( $(this), type ); | |
$parent.attr( attributes ).removeAttr( 'data-expand-target' ); | |
changeType( $parent, 'a' ); | |
}); | |
} | |
applyExpandLink(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
En gros, le but de ce script est d'étendre les liens en javascript :
Les liens ayant l’attribut data-expand-target peuvent avoir une ancre courte, et au chargement de la page se déplacer sur un parent plus global ayant l’attribut data-expand-link, le but bien sûr étant d'optimiser le SEO sans sacrifier l'ergonomie.
Je le teste sur Wabeo depuis 3 ans sans aucun soucis.