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('你好,百度')
  });
}

 

posted on 2014-11-29 22:19  水手paul2008  阅读(690)  评论(1编辑  收藏  举报