Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript How to remove script for responsive media queries?

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #264005
    PicnicTutorials
    Participant

    Hello. I just need to remove a certain JavaScript block when the screen size gets below a whatever value. I got the media queries and all that jazz down pat I just don’t know how to stop that JavaScript. Thanks.

    #264009
    JeroenR
    Participant

    Check this: https://stackoverflow.com/questions/1248081/get-the-browser-viewport-dimensions-with-javascript

    You can use a condition on the width of your window maybe in your javascript?

    #264029
    bclovis
    Participant

    If I need to match a media query in css, I usually do a window.matchMedia check:

    https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia

    #264064
    PicnicTutorials
    Participant

    Thanks guys. So I found a solution.

    Using jquery then say…

    if ( $(window).width() > 739) {
    //Add your javascript for large screens here
    }
    else {
    //Add your javascript for small screens here
    }

    Question though. Does the above do the exact same thing as the code below? Or is one better

    if (window.matchMedia(“(min-width: 400px)”).matches) {
    /* the viewport is at least 400 pixels wide /
    } else {
    /
    the viewport is less than 400 pixels wide */
    }

    #264065
    PicnicTutorials
    Participant

    aside from the different values of course

    #264073
    Shikkediel
    Participant

    Not exactly… when there’s a consistent scrollbar, it is included in the window width but not necessarily in the media query. But there won’t be scrollbars on mobile screens so it’ll have the same outcome.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.