从URL调用Javascript函数

从URL调用Javascript函数,javascript,Javascript,下面的javascript支持向左/向右滑动导航。出于某种原因,我想使用URL调用此函数,例如。某些URL相当于左扫,而另一个URL相当于右扫。这可能吗 (function( window ){ var window = window, document = window.document, screen = window.screen, touchSwipeListener = function( options ) { // Private memb

下面的javascript支持向左/向右滑动导航。出于某种原因,我想使用URL调用此函数,例如
。某些URL相当于左扫,而另一个URL相当于右扫。这可能吗

(function( window ){

var window = window,
    document = window.document,
    screen = window.screen,
    touchSwipeListener = function( options ) {
        // Private members
        var track = {
                startX: 0,
                endX: 0
            },
            defaultOptions = {
                moveHandler: function( direction ) {},
                endHandler: function( direction ) {},
                minLengthRatio: 0.3
            },
            getDirection = function() {
                return track.endX > track.startX ? "prev" : "next";
            },
            isDeliberateMove = function() {
                var minLength = Math.ceil( screen.width * options.minLengthRatio );
                return Math.abs(track.endX - track.startX) > minLength;
            },
            extendOptions = function() {
                for (var prop in defaultOptions) {
                    if ( defaultOptions.hasOwnProperty( prop ) ) {
                        options[ prop ] || ( options[ prop ] = defaultOptions[ prop ] );
                    }
                }
            },
            handler = {
                touchStart: function( event ) {
                    // At least one finger has touched the screen
                    if ( event.touches.length > 0  ) {
                        track.startX = event.touches[0].pageX;
                    }
                },
                touchMove: function( event ) {
                    if ( event.touches.length > 0  ) {
                        track.endX = event.touches[0].pageX;
                        options.moveHandler( getDirection(), isDeliberateMove() );
                    }
                },
                touchEnd: function( event ) {
                    var touches = event.changedTouches || event.touches;
                    if ( touches.length > 0  ) {
                        track.endX = touches[0].pageX;
                        isDeliberateMove() && options.endHandler( getDirection() );
                    }
                }
            };

        extendOptions();
        // Graceful degradation
        if ( !document.addEventListener ) {
            return {
                on: function() {},
                off: function() {}
            }
        }
        return {
            on: function() {
                document.addEventListener('touchstart', handler.touchStart, false);
                document.addEventListener('touchmove', handler.touchMove, false);
                document.addEventListener('touchend', handler.touchEnd, false);
            },
            off: function() {
                document.removeEventListener('touchstart', handler.touchStart);
                document.removeEventListener('touchmove', handler.touchMove);
                document.removeEventListener('touchend', handler.touchEnd);
            }
        }
    }
    // Expose global
    window.touchSwipeListener = touchSwipeListener;

}( window ));



(function( window ){
    var document = window.document,
        // Element helpers
        Util = {
            addClass: function( el, className ) {
                el.className += " " + className;
            },
            hasClass: function( el, className ) {
                var re = new RegExp("\s?" + className, "gi");
                return re.test( el.className );
            },
            removeClass: function( el, className ) {
                var re = new RegExp("\s?" + className, "gi");
                el.className = el.className.replace(re, "");
            }
        },
        swipePageNav = (function() {
            // Page sibling links like <link rel="prev" title=".." href=".." />
            // See also http://diveintohtml5.info/semantics.html
            var elLink = {
                    prev: null,
                    next: null
                },
                // Arrows, which slide in to indicate the shift direction
                elArrow = {
                    prev: null,
                    next: null
                },
                swipeListener;
            return {
                init: function() {
                    this.retrievePageSiblings();
                    // Swipe navigation makes sense only if any of sibling page link available
                    if ( !elLink.prev && !elLink.next ) {
                        return;
                    }
                    this.renderArows();
                    this.syncUI();
                },
                syncUI: function() {
                    var that = this;
                    // Assign handlers for swipe "in progress" / "complete" events
                    swipeListener = new window.touchSwipeListener({
                       moveHandler: function( direction, isDeliberateMove ) {
                           if ( isDeliberateMove ) {
                               if ( elArrow[ direction ] && elLink[ direction ] ) {
                                   Util.hasClass( elArrow[ direction ], "visible" ) ||
                                       Util.addClass( elArrow[ direction ], "visible" );
                               }
                           } else {
                               Util.removeClass( elArrow.next, "visible" );
                               Util.removeClass( elArrow.prev, "visible" );
                           }
                       },
                       endHandler: function( direction ) {
                            that[ direction ] && that[ direction ]();
                       }
                    });
                    swipeListener.on();
                },
                retrievePageSiblings: function() {
                    elLink.prev = document.querySelector( "head > link[rel=prev]");
                    elLink.next = document.querySelector( "head > link[rel=next]");
                },
                renderArows: function() {
                    var renderArrow = function( direction ) {
                        var div = document.createElement("div");
                        div.className = "spn-direction-sign " + direction;
                        document.getElementsByTagName( "body" )[ 0 ].appendChild( div );
                        return div;
                    }
                    elArrow.next = renderArrow( "next" );
                    elArrow.prev = renderArrow( "prev" );
                },
                // When the shift (page swap) is requested, this overlay indicates that
                // the current page is frozen and a new one is loading
                showLoadingScreen: function() {
                    var div = document.createElement("div");
                    div.className = "spn-freezing-overlay";
                    document.getElementsByTagName( "body" )[ 0 ].appendChild( div );
                },
                // Request the previous sibling page
                prev: function() {
                    if ( elLink.prev ) {
                        this.showLoadingScreen();
                        window.location.href = elLink.prev.href;
                    }
                },
                // Request the next sibling page
                next: function() {
                    if ( elLink.next ) {
                        this.showLoadingScreen();
                        window.location.href = elLink.next.href;
                    }
                }
            }
        }())

    // Apply when document is ready
    document.addEventListener( "DOMContentLoaded", function(){
        document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
        try {
            swipePageNav.init();
        } catch (e) {
            alert(e);
        }
    }, false );


}( window ));
(功能(窗口){
var窗口=窗口,
document=window.document,
screen=window.screen,
TouchWipeListener=功能(选项){
//私人成员
可变磁道={
startX:0,
endX:0
},
默认选项={
moveHandler:函数(方向){},
endHandler:函数(方向){},
minLengthRatio:0.3
},
getDirection=函数(){
返回track.endX>track.startX?“上一个”:“下一个”;
},
IsDelarateMove=函数(){
var minLength=Math.ceil(screen.width*options.minLengthRatio);
返回Math.abs(track.endX-track.startX)>minLength;
},
extendOptions=函数(){
for(默认选项中的var prop){
if(defaultOptions.hasOwnProperty(prop)){
选项[prop]| |(选项[prop]=默认选项[prop]);
}
}
},
处理程序={
touchStart:功能(事件){
//至少有一根手指触到了屏幕
如果(event.touchs.length>0){
track.startX=event.touchs[0].pageX;
}
},
touchMove:功能(事件){
如果(event.touchs.length>0){
track.endX=event.touchs[0].pageX;
options.moveHandler(getDirection(),isDeliveRateMove());
}
},
touchEnd:功能(事件){
var touchs=event.changedtouchs | | event.touchs;
如果(长度>0){
track.endX=触摸[0].pageX;
isDelarateMove()&&options.endHandler(getDirection());
}
}
};
扩展();
//优雅退化
如果(!document.addEventListener){
返回{
关于:函数(){},
关闭:函数(){}
}
}
返回{
关于:函数(){
document.addEventListener('touchstart',handler.touchstart,false);
document.addEventListener('touchmove',handler.touchmove,false);
document.addEventListener('touchend',handler.touchend,false);
},
关闭:函数(){
document.removeEventListener('touchstart',handler.touchstart);
document.removeEventListener('touchmove',handler.touchmove);
document.removeEventListener('touchend',handler.touchend);
}
}
}
//暴露全球
window.touchWipeListener=touchWipeListener;
}(窗口);
(功能(窗口){
var document=window.document,
//元素助手
Util={
addClass:函数(el,类名){
el.className+=“”+className;
},
hasClass:函数(el,类名){
var re=new RegExp(“\s?”+className,“gi”);
返回重新测试(el.className);
},
removeClass:函数(el,类名){
var re=new RegExp(“\s?”+className,“gi”);
el.className=el.className.replace(重“”);
}
},
swipePageNav=(函数(){
//页面兄弟链接,如
//另见http://diveintohtml5.info/semantics.html
var elLink={
prev:null,
下一个:空
},
//箭头,向内滑动以指示换档方向
elArrow={
prev:null,
下一个:空
},
swipeListener;
返回{
init:function(){
this.retrievePageSides();
//只有当任何同级页面链接可用时,滑动导航才有意义
如果(!elLink.prev&&!elLink.next){
返回;
}
这个.renderaws();
this.syncUI();
},
syncUI:function(){
var=这个;
//为刷卡“进行中”/“完成”事件分配处理程序
swipeListener=new window.touchWipeListener({
moveHandler:函数(方向,IsDeliveRateMove){
如果(IsDeliveRateMove){
if(elArrow[方向]&&elLink[方向]){
Util.hasClass(elArrow[方向],“可见”)||
Util.addClass(elArrow[方向],“可见”);
}
}否则{
Util.removeClass(elArrow.next,“可见”);
Util.removeClass(elArrow.prev,“可见”);
}
},
endHandler:函数(方向){
那[方向]&&那[方向]();
}
});
swipeListener.on();
},
retrievePageSides:function(){
elLink.prev=document.querySelector(“head>link[rel=prev]”);
艾琳