Skip to content

Instantly share code, notes, and snippets.

@robspangler
Last active December 16, 2015 20:19
Show Gist options
  • Save robspangler/5491715 to your computer and use it in GitHub Desktop.
Save robspangler/5491715 to your computer and use it in GitHub Desktop.
Open a unique URL from an <a> tag when on mobile browser. Used for responsive designs.Example: <a href="http://desktop.url" data-mobile-href="http://mobile.url">Link Text</a>
jQuery(document).ready(function($){
//Max Mobile Size
var mobileMaxWidth = 570;
//DO ON CLICK (Any mobile specific links go to the URL in their data-mobile-href -- on 'click' allows it to detect the current screensize)
$('*[data-mobile-href]').click(function(){
if ( $(document).width() < mobileMaxWidth ) {
var mobileURL = $(this).data("mobile-href");
$(this).attr('href', mobileURL ); //swap default href with new mobile href
window.location.href = mobileURL; //send window to new url
return false;
}
});
//OR, DO ONE TIME
if ( $(document).width() < mobileMaxWidth ) {
$('*[data-mobile-href]').each(function(){
$(this).attr('href', $(this).data("mobile-href") );
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment