I'm new to jQuery and I found this code to use in my site:
$(function() {
elemWidth = parseInt(getComputedStyle(document.querySelector("#checkVw"), null).width, 10);
halfWidth = parseInt(window.innerWidth / 2, 10);
alert("Your browser " + ((elemWidth == halfWidth) ? "" : "does not ") + "support VW and VH");
});
The code check if my element have the size that I define in css, returning if users browser support my size unit (viewport).
What I need is load a script if elemWidth value is not the same as halfWidth, but I don't know how to change this alert to a conditioning function.
Thanks!
The expression ((elemWidth == halfWidth) ? "" : "does not "
makes use of the conditional ternary operator.
It is short for:
if (elemWidth == halfWidth) {
// ""
} else {
// "does not"
}
Therefore if you want to load a script if the elemWidth
value is not the same as halfWidth
, then you could simply use the $.getScript
function or whatever you are using the load the script.
if (elemWidth !== halfWidth) {
$.getScript('...');
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments