Last active
December 2, 2016 23:08
-
-
Save petrknap/e23834deff7d11bb516e to your computer and use it in GitHub Desktop.
JS function which returns active device detected by Bootstrap
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
/.idea/ |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<title>getBootstrapDevice.js demo</title> | |
<link rel="stylesheet" | |
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" | |
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" | |
crossorigin="anonymous"> | |
</head> | |
<body> | |
<dl> | |
<dt>CSS</dt> | |
<dd> | |
<span class="visible-xs">xs</span> | |
<span class="visible-sm">sm</span> | |
<span class="visible-md">md</span> | |
<span class="visible-lg">lg</span> | |
</dd> | |
<dt>JavaScript</dt> | |
<dd> | |
<span id="insertDeviceHere">unknown</span> | |
</dd> | |
</dl> | |
<script type="application/javascript" | |
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js" | |
integrity="sha256-rsPUGdUPBXgalvIj4YKJrrUlmLXbOb6Cp7cdxn1qeUc=" | |
crossorigin="anonymous"></script> | |
<script type="application/javascript" | |
src="getBootstrapDevice.js"></script> | |
<script> | |
$(function () { | |
var prevDevice = null, onResize = function () { | |
var device = getBootstrapDevice(); | |
if (device != prevDevice) { | |
prevDevice = device; | |
onDeviceChange(device); | |
} | |
}, onDeviceChange = function (device) { | |
$("#insertDeviceHere").html(device); // replace it by your code | |
}; | |
$(window).resize(onResize); | |
onResize(); | |
}); | |
</script> | |
</body> | |
</html> |
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
/** | |
* Returns active device detected by Bootstrap | |
* | |
* @author Petr Knap <dev@petrknap.cz> | |
* | |
* @param {String} [testerId="getBootstrapDevice"] Identifier used for prepended div element | |
* @param {String} [defaultDevice="lg"] Default device | |
* | |
* @returns {String} Active device (xs|sm|md|lg) | |
*/ | |
function getBootstrapDevice(testerId, defaultDevice) { | |
var $body, $tester; | |
if(!testerId) { | |
testerId = "getBootstrapDevice"; | |
} | |
if(!defaultDevice) { | |
defaultDevice = "lg"; | |
} | |
$body = $("body"); | |
$tester = $body.find("#" + testerId); | |
if($tester.length == 0) { // Insert tester | |
$body.prepend('' + | |
'<div id="' + testerId + '" style="position: absolute; z-index: -999">' + | |
' <div class="visible-xs-block"> </div>' + | |
' <div class="visible-sm-block"> </div>' + | |
' <div class="visible-md-block"> </div>' + | |
' <div class="visible-lg-block"> </div>' + | |
'</div>' + | |
''); | |
return getBootstrapDevice(testerId, defaultDevice); | |
} | |
else { | |
$tester.find("div").each(function() { | |
if($(this).css("display") == "block") { | |
defaultDevice = $(this).attr('class').split("-")[1]; | |
return false; // Break the loop | |
} | |
}); | |
return defaultDevice; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment