duanlei0282 2016-04-05 22:24
浏览 98
已采纳

Wordpress - 我应该将脚本分成特定页面还是有一个主js文件?

This is more of a general performance question. On my wordpress projects I will generally have an app.js file which is like a master collection of all of my little js scripts.

Some of these scripts will be specific to a certain page for example 'Our Brands'. At the moment what I do is check for the page title with localised variables, so for example:

if (pageTitle == 'our-brands') {

    function loopThroughCharCode(loopStart, loopEnd) {
        var i = loopStart;
        for (var i = loopStart; i <= loopEnd; i++) {
            var letter = String.fromCharCode(i);
                if (i == 49) {
                    // if number 1, use the # symbol instead of 1
                    $('li > a#' + letter).first().parent().before('<li class="full-width">#</li>');
                } else {
                    $('li > a#' + letter).first().parent().before('<li class="full-width">' + letter +'</li>');
                }
        }
    }
    loopThroughCharCode(65, 90); // loop through a to z
    loopThroughCharCode(49, 49); // number 1

}

Is this better than seperating this block of code into its own js file and only enqueueing it on the our brands page?

FYI I minify and combine all my js on the fly using a plugin.

  • 写回答

2条回答 默认 最新

  • duankang5882 2016-04-06 01:26
    关注

    Since you minify and combine your JavaScript files in production, I will definately advice you to separate your files while developing. That way it's much simpler to edit and debug and it makes adding or deleting a page much simpler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献