3

I want to dissable javascripts in media query.

@media only screen
 (max-device-width : 568px) {
.sidebar { display: none; } 
  #navmenu {  margin-top:0px; width:auto; padding:10px; margin-left:15%;}
.content {width:auto; height:auto; margin-left:0; margin-top:3%;}
.contentsing{position:relative; width:100%; height:auto; margin-left:0%; margin-top:-150px;}


}

That is mu code, now i want to add something that will disable scripts.

3
  • Why do you want to do that? Do you want to disable your own scripts? Commented Dec 18, 2013 at 13:23
  • Yes.. here is link [link]stefanvblog.com/category/news Now i want to disable that script with menu on mobile device. Commented Dec 18, 2013 at 13:25
  • Well CSS can not disable JavaScript so you need to make your JavaScript detect that state. Commented Dec 18, 2013 at 13:31

3 Answers 3

9

You could wrap your script in an if statement that checks how wide the screen is like this:

if(window.innerWidth > 568){
    ...execute script
}

However, that will only execute once, so what if you resize your browser window? You could have an event listener that executes your script whenever you resize the browser.

window.addEventListener('resize', function(){
    if(window.innerWidth > 568){
        ...execute script
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

When you use jQuery - this code will help you

$(window).resize(function() {
    if ($(window).width()>992){
        ...execute script
    }
});

Comments

1

this would work better....

if ($(window).width()>568){
    ...execute script
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.