首页 > 解决方案 > return evaluated javascript function to terminal

问题描述

I'm trying to return a function that has been evaluated to the terminal. I tried using window.onload(), console.log(); alert(); but the URL isn't evaluated and sent to the terminal.

I run the code below using the command phantomjs test.js

var webPage = require('webpage');
var page = webPage.create();

function testlink() {
  return(["h","t","t","p","s",":","\/","\/","i","b","m",".","c","o","m"]);
}

window.onload = testlink;
console.log(testlink);
alert(testlink);

Instead of getting (this is what I want)

https://ibm.com

I get

function testlink() {
  return(["h","t","t","p","s",":","\/","\/","i","b","m",".","c","o","m"]);
}

Ps: I'm using Ubuntu 18.04 and phantomjs 2.1.1

标签: javascriptphantomjs

解决方案


尝试做console.log(testlink())而不是console.log(testlink)

如果我们想加入该数组中的所有字符,只需返回:

["h","t","t","p","s",":","\/","\/","i","b","m",".","c","o","m"].join("")

或者,如果您不想更改功能,请执行console.log(testlink().join(""))


推荐阅读