Using jQuery function if element width is valid

Zeca Novaes

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!

Josh Crozier

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculate width of element using jQuery

From Dev

Use JQuery each function to return element width

From Dev

Increase element width by percentage using jQuery

From Dev

Stop jQuery function using screen width

From Dev

Changing the width of HTML div element automatically using jQuery

From Dev

Using Jquery wrap function with absolute placed element

From Dev

Angular element.width is not a function?

From Dev

Incorrect jQuery element width calculation

From Dev

jQuery width of element added in loop

From Dev

How to hide element using Class; instead of using css function in JQuery?

From Dev

How to hide element using Class; instead of using css function in JQuery?

From Dev

How can I dynamically apply the "left" px to a div's width, within the same parent element, using jQuery

From Dev

How can I find the intended render width of an element using jQuery before it is rendered?

From Dev

Display the biggest width of element among other three elements inside a Div using Jquery

From Dev

How can I call a function after appending an element using jquery?

From Dev

Get the closest element to a SVG node using Jquery closest() function

From Dev

How to get Id of an element using jQuery Closest() function

From Dev

Add function to a jQuery element

From Dev

Passing element to jQuery function

From Dev

Using jquery to determine screen width

From Dev

Toggle Width of a Div Using Jquery

From Dev

Toggle width of a container using jQuery

From Dev

Toggle Width of a Div Using Jquery

From Dev

How to find a parent element width in JavaScript or jQuery?

From Dev

Animate the width of a block element as it changes with jQuery?

From Dev

jQuery, set element width minus size in em

From Dev

Jquery horizontal pan with 100% width element

From Dev

jQuery: change element position depending on screen width

From Dev

Jquery :Automatically change the width equal to an element

Related Related

  1. 1

    Calculate width of element using jQuery

  2. 2

    Use JQuery each function to return element width

  3. 3

    Increase element width by percentage using jQuery

  4. 4

    Stop jQuery function using screen width

  5. 5

    Changing the width of HTML div element automatically using jQuery

  6. 6

    Using Jquery wrap function with absolute placed element

  7. 7

    Angular element.width is not a function?

  8. 8

    Incorrect jQuery element width calculation

  9. 9

    jQuery width of element added in loop

  10. 10

    How to hide element using Class; instead of using css function in JQuery?

  11. 11

    How to hide element using Class; instead of using css function in JQuery?

  12. 12

    How can I dynamically apply the "left" px to a div's width, within the same parent element, using jQuery

  13. 13

    How can I find the intended render width of an element using jQuery before it is rendered?

  14. 14

    Display the biggest width of element among other three elements inside a Div using Jquery

  15. 15

    How can I call a function after appending an element using jquery?

  16. 16

    Get the closest element to a SVG node using Jquery closest() function

  17. 17

    How to get Id of an element using jQuery Closest() function

  18. 18

    Add function to a jQuery element

  19. 19

    Passing element to jQuery function

  20. 20

    Using jquery to determine screen width

  21. 21

    Toggle Width of a Div Using Jquery

  22. 22

    Toggle width of a container using jQuery

  23. 23

    Toggle Width of a Div Using Jquery

  24. 24

    How to find a parent element width in JavaScript or jQuery?

  25. 25

    Animate the width of a block element as it changes with jQuery?

  26. 26

    jQuery, set element width minus size in em

  27. 27

    Jquery horizontal pan with 100% width element

  28. 28

    jQuery: change element position depending on screen width

  29. 29

    Jquery :Automatically change the width equal to an element

HotTag

Archive