0

HI friends I have to disable script for parallax effects when open on mobile phones. I found a script similar to what I wanted and adapted, however it does not work. Anyone know what's wrong?

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

jQuery(document).ready(function(){
if( !isMobile.any()){
    $(window).stellar(): false;
    }
});

4 Answers4

2

What is the : false after $(window).stellar()? I'm not sure what you're trying to do there, or if it's just some kind of copy/paste error or something, but that will give:

SyntaxError: Unexpected token :

I think you want this:

jQuery(document).ready(function(){
    if( !isMobile.any()){
       $(window).stellar();
    }
});
nnnnnn
  • 138,378
  • 23
  • 180
  • 229
1
jQuery(document).ready(function(){
if( !isMobile.any()){
    $(window).stellar(): false;
    }
});

That third line is syntactically wrong. What exactly are you trying to achieve with something like
fn(): false;?

I believe you're looking for:

jQuery(document).ready(function(){
    if( !isMobile.any() ){
        $(window).stellar();
    }
});

In other words: only if !isMobile.any() should $(window).stellar() be executed. In the other case (where isMobile.any() is true), the if block should not be executed.

Mattias Buelens
  • 17,720
  • 4
  • 40
  • 49
0

Open up the console in your browser.

Go to this fiddle: http://jsfiddle.net/vyPjx/

You will see the error

Uncaught SyntaxError: Unexpected token : 

The error will point to the line

$(window).stellar(): false;

Looks like you were trying to use a ternary operator but you are not?

It should be just

$(window).stellar();

If you were going the ternary route it would have been

jQuery(document).ready(function(){
    var hasRun = !isMobile.any() ? $(window).stellar() : false;    
});
epascarello
  • 185,306
  • 18
  • 175
  • 214
-1

Try this,

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    }
};
var any=(isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());

jQuery(document).ready(function(){
    if(!any){
        alert('No mobiles');
        $(window).stellar();
    }
});
Rohan Kumar
  • 38,998
  • 11
  • 69
  • 99
  • 1
    You commented out the problem! Why did you moved code out of the namespace! That was not the issue! – epascarello Jun 04 '13 at 13:06
  • 1
    `if(!any)` won't work. You mean `if(!any())`. (Though as epascarello said, that function wasn't the issue.) – nnnnnn Jun 04 '13 at 13:07
  • 1
    @nnnnnn Here any is like a variable it can be used like `if(!any)`. Read http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname – Rohan Kumar Jun 04 '13 at 13:18
  • I'm familiar with the concept of assigning a function to a variable. But if you want to _invoke_ the function you still need parentheses as in `if(!any())`. If you say `if(!any)` that will test whether the variable itself is falsey, which it never will be if it references a function. (Test your code and see...) Note also that in this case `any` doesn't need to be a function because you removed it from the `isMobile` "namespace", so you can just say `var any = isMobile.Android() || etc`. – nnnnnn Jun 04 '13 at 14:05