te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - reference multiple js files in a single line in a html file - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - reference multiple js files in a single line in a html file - Stack Overflow

programmeradmin3浏览0评论

Is there an easy way to reference all js files in an HTML file rather than referencing it one by one?

Instead of this -

<script type="text/javascript" src="js/controllers/mainCtrl.js"></script>
<script type="text/javascript" src="js/controllers/browseCtrl.js"></script>
...

I'm looking for something like this -

<script type="text/javascript" src="js/controllers/*.js"></script>

Or is there a tool out there that copies the contents of these files into one file and reference that one file instead? This will be minimize the HTTP calls.

Is there an easy way to reference all js files in an HTML file rather than referencing it one by one?

Instead of this -

<script type="text/javascript" src="js/controllers/mainCtrl.js"></script>
<script type="text/javascript" src="js/controllers/browseCtrl.js"></script>
...

I'm looking for something like this -

<script type="text/javascript" src="js/controllers/*.js"></script>

Or is there a tool out there that copies the contents of these files into one file and reference that one file instead? This will be minimize the HTTP calls.

Share Improve this question asked Apr 11, 2013 at 14:38 tempidtempid 8,22828 gold badges73 silver badges104 bronze badges 1
  • Unless your server supports something like this, it won't work. It is not a browser issue and should add a tag for the server technology you are using to see if there is a solution. – lucuma Commented Apr 11, 2013 at 14:42
Add a ment  | 

6 Answers 6

Reset to default 4

Is there an easy way to reference all js files in an HTML file rather than referencing it one by one?

For some value of "easy". There is nothing built in to browsers that will do it though.

Or is there a tool out there that copies the contents of these files into one file and reference that one file instead?

There are many. cat is the simplest one.

Call it from your usual build tool.

You can use something like require.js to bine them at runtime during development, and call r.js via Node from your build tool for packaging for your staging and live environments.

You can give Require.js a go. Require.js is the only JavaScript-file that is loaded through the script-tag. When you go out of development you can use Require.js's r.js to minify and concat everything into one file.

I use this tool all the time to minify my JS files:

Online Javascript Compression Tool

You can upload multiple files and it will concatenate them into one for you. It also produces smaller filesizes than YUI pressor, and Google's JS piler most of the time too.

I am not sure why this hasn't been mentioned yet, but I do suppose this thread is a bit dated. Since I stumbled on this during my search to solve this very problem, I thought I would put a quick write-up about GruntJS here for other newbie JS guys to find.

Essentially, a properly configured Gruntfile.js will be able to perform a variety of tasks around JS including, but not limited to: concatenating files, minifying files, code linting, and much much more.

You can install grunt on Ubuntu with:

$ sudo apt-get install nodejs
$ sudo npm -g install grunt-cli
$ cd /path/to/my/project
--- Assumming you have a package.json file already in place ---
$ npm install grunt --save-dev
--- Install grunt plugins you wish to use ---
$ npm install grunt-contrib-concat --save-dev
$ npm install grunt-contrib-uglify --save-dev
$ npm install grunt-contrib-jshint --save-dev
$ npm install grunt-contrib-watch --save-dev

On the GruntJS site, there is a pretty good write-up for how to use GruntJS, but here is an example Gruntfile.js that will:

  1. Lint all the JS files (app.js in the current directory and all .js files in the ngmodules directory).
  2. Concatenate and save the files to dist/package-name.js.
  3. Minify the concatenated file and save it to dist/package-name.min.js.

Gruntfile.js:

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concat: {
      options: {
        separator: ';'
      },
      dist: {
        src: ['app.js', 'ngmodules/**/*.js'],
        dest: 'dist/<%= pkg.name %>.js'
      }
    },
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
      },
      dist: {
        files: {
          'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
        }
      }
    },
    jshint: {
      files: ['Gruntfile.js', 'app.js', 'ngmodules/**/*.js'],
      options: {
        // options here to override JSHint defaults
        globals: {
          jQuery: true,
          console: true,
          module: true,
          document: true
        }
      }
    },
    watch: {
      files: ['<%= jshint.files %>'],
      tasks: ['jshint']
    }
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-concat');

  grunt.registerTask('default', ['jshint', 'concat', 'uglify']);

};

There is also Gulp (http://gulpjs./), which can be used with various plugins. Here's an example that concatenates *.js files in one single file (main.js), then renames the resulting file and finally minifies it:

var gulp = require('gulp'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat');

gulp.task('scripts', function(){
return gulp.src('./src/js/*.js')
.pipe(concat('main.js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('./src/js/*.js'));

You can try and bine your javascript files or plugins into one:

<script type="text/javascript" src="js/controllers/plugins.js"></script>

You'll have to do it manually though. Another option would be to write a server-side script to bine and minify all your javascript files.

发布评论

评论列表(0)

  1. 暂无评论