casperjs在拆分文件后的中文乱码问题的解决

windows环境。

capserjs的中文乱码使用phantom.outputEncoding="GBK";即可解决。

但当我们脚本很大,需要拆分时(参考http://docs.casperjs.org/en/latest/writing_modules.html),拆分到另一文件(example.js)时,中文却无法echo出来。

后来发现, example.js是我们用记事本建立的,编码为ANSI,改为UTF-8编码即可正确显示中文。

延伸,若任意脚本为ansi编码,echo中文都会无法显示。示例如下。

main.js

phantom.outputEncoding="GBK";

var casper = require('casper').create();

var example= require('./example.js')

casper.echo('main你好');
example.say(); casper.run();

拆分的 example.js

var require = patchRequire(require);

exports.say = function () {
	casper.start('http://baidu.com',function then(){
		casper.echo('你好,百度')
  });
}
原文地址:https://www.cnblogs.com/wigis/p/4132111.html