我正在尝试上班的脚本。我有一个网站,您可以点击"完整网站"从移动版本中剥离移动样式并以幻灯片形式加载所有幻灯片。链接只是"?mobile = off"对于你所处的任何页面。所以我编写了一个脚本来从URL中获取该参数,然后使用if else语句。这就是我拥有的东西,它不起作用。即使满足正确的条件,它总是会转到语句的else部分,因此我不认为它在if语句中获取param值。如果我只使用if(Modernizr.mq'(max-width:1000px)')而没有if语句中的urlParams,它确实有效。
var urlParams;
(window.onpopstate = function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
if ( (Modernizr.mq('(max-width: 1000px)')) && (urlParams[mobile] == ('undefined')) ) {
//this stuff removes all except first slide in flexslider for mobile
$(".flexslider li:not(.keep)").remove();
$( ".flexslider" ).removeClass( "flexslider loading" ).addClass( "noslider" );
$( ".slides" ).removeClass( "slides" ).addClass( "noslides" );
} else {
//strips off mobile stylesheet and loads all flexslider slides
$("#mobile").remove();
$(window).load(function(){
$('.flexslider').flexslider({
animation: "fade",
animationSpeed: 3500,
directionNav: false,
controlNav: false,
smoothHeight: false,
slideshow: false,
useCss: false,
start: function(slider){
$('body').removeClass('loading');
}
});
});
$('.prev, .next').on('click', function(){
var href = $(this).attr('href');
$('.flexslider').flexslider(href)
return false;
});
}
答案 0 :(得分:0)
将您的if语句更改为:
if ( (Modernizr.mq('(max-width: 1000px)')) && (urlParams[mobile] === undefined) ) {
你也可以这样做:
if ( (Modernizr.mq('(max-width: 1000px)')) && (typeof(urlParams[mobile]) == 'undefined') ) {