pyv8学习python和javascript变量进行交互

WBOY
Release: 2016-06-16 08:45:59
Original
1467 people have browsed it

python取得javascript里面的值

复制代码 代码如下:

import PyV8

with PyV8.JSContext() as env1:
    env1.eval("""
                var_i = 1;
                var_f = 1.0;
                var_s = "test";
                var_b = true;
            """)
    vars = env1.locals
    var_i = vars.var_i
    print var_i


javascript取得python里面的值

复制代码 代码如下:

import PyV8

with PyV8.JSContext() as env1:
    env1.securityToken = "foo"
    env1.locals.prop = 3
    print int(env1.eval("prop"))

python和javascript里面的函数交互

python调用javascript里面的函数 python调用func就可以使用js里面的function函数了

复制代码 代码如下:

import PyV8

with PyV8.JSContext() as ctxt:
    func = ctxt.eval("""
                (function ()
                {
                    function a()
                    {
                        return "abc";
                    }

                    return a();
                })
                """)
    print func()

这样也可以

复制代码 代码如下:

import PyV8

with PyV8.JSContext() as ctxt:
    func = ctxt.eval("""
                    function a()
                    {
                        return "abc";
                    }

                    function c()
                    {
                        return "abc";
                    }
                """)
    a = ctxt.locals.a
    print a()

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template