通过script.js在WordPress中包含多个文件

时间:2018-05-07 作者:Burhan Ali

我有剧本。用于html到wordpress转换的js文件。在html中它可以工作,但在wordpress中它不工作。该文件包含滑块、菜单等,我可以在没有该文件的情况下将html转换为wordpress,该文件包含以下内容

function include(scriptUrl) {
    document.write(\'<script src="\' + scriptUrl + \'"></script>\');
}

function isIE() {
    var myNav = navigator.userAgent.toLowerCase();
    return (myNav.indexOf(\'msie\') != -1) ? parseInt(myNav.split(\'msie\')[1]) : false;
};

/* cookie.JS
 ========================================================*/
include(\'js/jquery.cookie.js\');

/* Easing library
 ========================================================*/
include(\'js/jquery.easing.1.3.js\');

/* Stick up menus
 ========================================================*/
;
(function ($) {
    var o = $(\'html\');
    if (o.hasClass(\'desktop\')) {
        include(\'js/tmstickup.js\');

        $(document).ready(function () {
            $(\'#stuck_container\').TMStickUp({})
        });
    }
})(jQuery);

/* ToTop
 ========================================================*/
;
(function ($) {
    var o = $(\'html\');
    if (o.hasClass(\'desktop\')) {
        include(\'js/jquery.ui.totop.js\');

        $(document).ready(function () {
            $().UItoTop({
                easingType: \'easeOutQuart\',
                containerClass: \'toTop fa fa-arrow-up\'
            });
        });
    }
})(jQuery);

/* EqualHeights
 ========================================================*/
;
(function ($) {
    var o = $(\'[data-equal-group]\');
    if (o.length > 0) {
        include(\'js/jquery.equalheights.js\');
    }
})(jQuery);

/* SMOOTH SCROLLIG
 ========================================================*/
;
(function ($) {
    var o = $(\'html\');
    if (o.hasClass(\'desktop\')) {
        include(\'js/jquery.mousewheel.min.js\');
        include(\'js/jquery.simplr.smoothscroll.min.js\');

        $(document).ready(function () {
            $.srSmoothscroll({
                step: 150,
                speed: 800
            });
        });
    }
})(jQuery);

/* Copyright Year
 ========================================================*/
;
(function ($) {
    var currentYear = (new Date).getFullYear();
    $(document).ready(function () {
        $("#copyright-year").text((new Date).getFullYear());
    });
})(jQuery);


/* Superfish menus
 ========================================================*/
;
(function ($) {
    include(\'js/superfish.js\');    
})(jQuery);

/* Navbar
 ========================================================*/
;
(function ($) {
    include(\'js/jquery.rd-navbar.js\');
})(jQuery);

/* Camera
========================================================*/
;(function ($) {
var o = $(\'#camera\');
    if (o.length > 0) {
        if (!(isIE() && (isIE() > 9))) {
            include(\'js/jquery.mobile.customized.min.js\');
        }

        include(\'js/camera.js\');

        $(document).ready(function () {
            o.camera({
                autoAdvance: true,
                height: \'25.8536%\',
                minHeight: \'300px\',
                pagination: true,
                thumbnails: false,
                playPause: false,
                hover: false,
                loader: \'none\',
                navigation: false,
                navigationHover: false,
                mobileNavHover: false,
                fx: \'simpleFade\'
            })
        });
    }
})(jQuery);

/* FancyBox
========================================================*/
;(function ($) {
    var o = $(\'.thumb\');
    if (o.length > 0) {
        include(\'js/jquery.fancybox.js\');
        include(\'js/jquery.fancybox-media.js\');
        include(\'js/jquery.fancybox-buttons.js\');
        $(document).ready(function () {
            o.fancybox();
        });
    }
})(jQuery);

/* Parallax 
=============================================*/ 
;(function ($) { 
    include(\'js/jquery.rd-parallax.js\'); 
})(jQuery);


/* Google Map
 ========================================================*/
;
(function ($) {
    var o = document.getElementById("google-map");
    if (o) {
        include(\'//maps.google.com/maps/api/js?sensor=false\');
        include(\'js/jquery.rd-google-map.js\');

        $(document).ready(function () {
            var o = $(\'#google-map\');
            if (o.length > 0) {
                var styleArray = [
                    {
                        "featureType": "administrative.locality",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#2c2e33"
                            },
                            {
                                "saturation": 7
                            },
                            {
                                "lightness": 19
                            },
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "landscape",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#ffffff"
                            },
                            {
                                "saturation": -100
                            },
                            {
                                "lightness": 100
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    },
                    {
                        "featureType": "poi",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#ffffff"
                            },
                            {
                                "saturation": -100
                            },
                            {
                                "lightness": 100
                            },
                            {
                                "visibility": "off"
                            }
                        ]
                    },
                    {
                        "featureType": "road",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "hue": "#bbc0c4"
                            },
                            {
                                "saturation": -93
                            },
                            {
                                "lightness": 31
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    },
                    {
                        "featureType": "road",
                        "elementType": "labels",
                        "stylers": [
                            {
                                "hue": "#bbc0c4"
                            },
                            {
                                "saturation": -93
                            },
                            {
                                "lightness": 31
                            },
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "road.arterial",
                        "elementType": "labels",
                        "stylers": [
                            {
                                "hue": "#bbc0c4"
                            },
                            {
                                "saturation": -93
                            },
                            {
                                "lightness": -2
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    },
                    {
                        "featureType": "road.local",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "hue": "#e9ebed"
                            },
                            {
                                "saturation": -90
                            },
                            {
                                "lightness": -8
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    },
                    {
                        "featureType": "transit",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#e9ebed"
                            },
                            {
                                "saturation": 10
                            },
                            {
                                "lightness": 69
                            },
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "water",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#e9ebed"
                            },
                            {
                                "saturation": -78
                            },
                            {
                                "lightness": 67
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    }
                ]

                o.googleMap({
                    styles: styleArray,
                });
            }
        });
    }
})
(jQuery);

(function ($) {
    var o = document.getElementById("google-map2");
    if (o) {
        include(\'//maps.google.com/maps/api/js?sensor=false\');
        include(\'js/jquery.rd-google-map.js\');

        $(document).ready(function () {
            var o = $(\'#google-map2\');
            if (o.length > 0) {
                var styleArray = [
                    {
                        "featureType": "administrative.locality",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#2c2e33"
                            },
                            {
                                "saturation": 7
                            },
                            {
                                "lightness": 19
                            },
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "landscape",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#ffffff"
                            },
                            {
                                "saturation": -100
                            },
                            {
                                "lightness": 100
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    },
                    {
                        "featureType": "poi",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#ffffff"
                            },
                            {
                                "saturation": -100
                            },
                            {
                                "lightness": 100
                            },
                            {
                                "visibility": "off"
                            }
                        ]
                    },
                    {
                        "featureType": "road",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "hue": "#bbc0c4"
                            },
                            {
                                "saturation": -93
                            },
                            {
                                "lightness": 31
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    },
                    {
                        "featureType": "road",
                        "elementType": "labels",
                        "stylers": [
                            {
                                "hue": "#bbc0c4"
                            },
                            {
                                "saturation": -93
                            },
                            {
                                "lightness": 31
                            },
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "road.arterial",
                        "elementType": "labels",
                        "stylers": [
                            {
                                "hue": "#bbc0c4"
                            },
                            {
                                "saturation": -93
                            },
                            {
                                "lightness": -2
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    },
                    {
                        "featureType": "road.local",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "hue": "#e9ebed"
                            },
                            {
                                "saturation": -90
                            },
                            {
                                "lightness": -8
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    },
                    {
                        "featureType": "transit",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#e9ebed"
                            },
                            {
                                "saturation": 10
                            },
                            {
                                "lightness": 69
                            },
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "water",
                        "elementType": "all",
                        "stylers": [
                            {
                                "hue": "#e9ebed"
                            },
                            {
                                "saturation": -78
                            },
                            {
                                "lightness": 67
                            },
                            {
                                "visibility": "simplified"
                            }
                        ]
                    }
                ]

                o.googleMap({
                    styles: styleArray,
                });
            }
        });
    }
})
(jQuery);

/* WOW
 ========================================================*/
;
(function ($) {
    var o = $(\'html\');

    if ((navigator.userAgent.toLowerCase().indexOf(\'msie\') == -1 ) || (isIE() && isIE() > 9)) {
        if (o.hasClass(\'desktop\')) {
            include(\'js/wow.js\');

            $(document).ready(function () {
                new WOW().init();
            });
        }
    }
})(jQuery);

/* Contact Form
 ========================================================*/
;
(function ($) {
    var o = $(\'#contact-form\');
    if (o.length > 0) {
        include(\'js/modal.js\');
        include(\'js/TMForm.js\'); 

        if($(\'#contact-form .recaptcha\').length > 0){
            include(\'../www.google.com/recaptcha/api/js/recaptcha_ajax.js\');
        }
    }
})(jQuery);

/* Search.js
 ========================================================*/
;
(function ($) {
    include(\'js/TMSearch.js\');
})(jQuery);

/* Orientation tablet fix
 ========================================================*/
$(function () {
    // IPad/IPhone
    var viewportmeta = document.querySelector && document.querySelector(\'meta[name="viewport"]\'),
        ua = navigator.userAgent,

        gestureStart = function () {
            viewportmeta.content = "width=device-width, minimum-scale=0.25, maximum-scale=1.6, initial-scale=1.0";
        },

        scaleFix = function () {
            if (viewportmeta && /iPhone|iPad/.test(ua) && !/Opera Mini/.test(ua)) {
                viewportmeta.content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0";
                document.addEventListener("gesturestart", gestureStart, false);
            }
        };

    scaleFix();
    // Menu Android
    if (window.orientation != undefined) {
        var regM = /ipod|ipad|iphone/gi,
            result = ua.match(regM);
        if (!result) {
            $(\'.sf-menus li\').each(function () {
                if ($(">ul", this)[0]) {
                    $(">a", this).toggle(
                        function () {
                            return false;
                        },
                        function () {
                            window.location.href = $(this).attr("href");
                        }
                    );
                }
            })
        }
    }
});
var ua = navigator.userAgent.toLocaleLowerCase(),
    regV = /ipod|ipad|iphone/gi,
    result = ua.match(regV),
    userScale = "";
if (!result) {
    userScale = ",user-scalable=0"
}
document.write(\'<meta name="viewport" content="width=device-width,initial-scale=1.0\' + userScale + \'">\');
我尝试了很多方法。我应该如何在wordpress中以适当的方式包含这些内容?

2 个回复
SO网友:WP Vlad

打开控制台(右键单击->检查-控制台)并读取错误,您可能没有将jQuery排入队列

您应该将脚本从functions.php (最佳实践)

function theme_scripts() {

  wp_enqueue_script( \'jquery\' );
  wp_enqueue_script( \'script\', get_template_directory_uri() . \'/script.js\', array(), false, true );

}
add_action( \'wp_enqueue_scripts\', \'theme_scripts\' );   

SO网友:Tom J Nowell

这不是WP问题,此功能是您的问题,也是许多其他您不知道的问题的原因:

function include(scriptUrl) {
    document.write(\'<script src="\' + scriptUrl + \'"></script>\');
}

....

include(\'js/jquery.cookie.js\');
这是一种糟糕的做法,而且容易出错:

每次发生这种情况时,当浏览器获取远程脚本时,所有渲染都会暂停。浏览器无法预加载脚本以提高性能,但更重要的是,该函数需要一个完整的URL,而您需要传递相对URL。这意味着每次更改页面时,浏览器试图加载的URL也会发生变化,

e、 g.对于js/jquery.cookie.js

主页上的example.com/js/jquery.cookie.js/about 它将尝试加载example.com/about/js/jquery.cookie.js/test 它将尝试加载example.com/test/js/jquery.cookie.jsinclude 函数,请执行以下选项之一:

注册所有库并将其排队,如果您必须有选择地将它们排队,请在PHP中进行检查,而不是在JS中进行检查,将它们全部捆绑起来,使用现代JS工具链,并使用Babel和Webpack将它们转换为捆绑包,这将允许您使用ES6语法和require 函数再次说明,这不是WP问题,这是始终存在的原始代码的问题,但现在出现了,因为您将文件移动到了服务器上相对于根的不同位置

此外,您应该查看浏览器开发工具。JS控制台将为所有这些错误抛出404个错误include 调用表示浏览器找不到JS文件,以及试图在其中找到该文件的完整URL

结束

相关推荐

未显示对Header.php的更改

我已经采取了一个新的可湿性粉剂网站,需要一些tlc。它有一堆我没用过的插件,其中之一就是“WP File manager”我对标题做了一个简单的更改。php-只是为了确保我在正确的地方。网站上出现了更改。然后我删除了更改,再次保存了文件,现在它仍然出现(在一些浏览器上,我有一个朋友以前从未访问过,我的更改仍然存在)。我已重新打开头文件,代码显示我的更改已被删除)任何人都可以建议发生了什么,以及如何让我对代码的修改得到可靠的反映。(顺便说一句,我要求ftp访问)谢谢爱德华