Skip to content

Instantly share code, notes, and snippets.

@zeshanshani
Last active December 13, 2016 04:18
Show Gist options
  • Save zeshanshani/82ddb606fee26b1b3ae0271ee4746b69 to your computer and use it in GitHub Desktop.
Save zeshanshani/82ddb606fee26b1b3ae0271ee4746b69 to your computer and use it in GitHub Desktop.
Adding Full Absolute URLs to the Anchor Links in the Navbar that are not on the same page. Works best with the parent menu and sub-menu links in the navbar. And for WordPress navbar especially.
/**
* Adding Full Absolute URLs to the Anchor Links in the Navbar that are not
* on the same page. Works best with the parent menu and sub-menu
* links in the navbar. And for WordPress navbar especially.
*
* Replace #parent_links_selector with the parent links selector
*/
jQuery(document).ready(function($) {
$('#parent_links_selector').each(function(i, el) {
var $el = $(el),
elHref = $el.attr('href'),
pathname = document.location.pathname;
if ( elHref.indexOf( pathname.slice(0,-1) ) < 0 ) {
$el.siblings('.sub-menu').find('a[href^="#"]').each(function(i, el) {
$(el).attr('href', function(i, href) {
return elHref + href;
});
}).on('click', function() {
return true;
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment