官术网_书友最值得收藏!

Inspecting the version at runtime

This recipe will introduce the global phantom object in PhantomJS and discuss how we can inspect the version at runtime using the version property.

Getting ready

To run this recipe, we will need a script that accesses phantom.version. The script in this recipe is available in the downloadable code repository as recipe01.js under chapter02. If we run the provided example script, we must change to the root directory for the book's sample code.

How to do it…

Consider the following script:

console.log('PhantomJS');
console.log('  - major version: ' + phantom.version.major);
console.log('  - minor version: ' + phantom.version.minor);
console.log('  - patch version: ' + phantom.version.patch);
phantom.exit();

Given the preceding script, enter the following at the command line:

phantomjs chapter02/recipe01.js

Our output should look like the following:

PhantomJS
 - major version: 1
 - minor version: 9
 - patch version: 2

How it works…

Our script operates by accessing the version object on the global phantom object and writing its properties (major, minor, and patch) to the console. The build of PhantomJS will have this metadata built in and exposed through phantom.version as read-only information.

Although our example is a trivial one, knowing the specific PhantomJS version at runtime can be helpful for building flexible scripts/libraries. For example, knowing the version at runtime can help us target preferred APIs while still falling back on older or deprecated ones in the event that those APIs are not available.

主站蜘蛛池模板: 阿坝| 青田县| 伊通| 河南省| 寿光市| 策勒县| 鸡西市| 沙坪坝区| 南涧| 陵川县| 浮梁县| 黄平县| 五家渠市| 江北区| 中山市| 宁安市| 大余县| 宜春市| 浦江县| 柳州市| 嘉禾县| 日照市| 方山县| 上栗县| 岢岚县| 农安县| 松江区| 卢氏县| 筠连县| 沧州市| 武鸣县| 龙泉市| 定南县| 淮北市| 福安市| 遂川县| 玉田县| 宾阳县| 清流县| 华池县| 宁德市|