获取内联变量的值 javascript

get the value of a varibale inside inline javascript

我正在构建 Web 应用程序,有时我需要从远程页面抓取名为 fid 的变量的值,该变量存在于脚本中,如下所示:

<script type="text/rocketscript">fid="mytarget"; v_width=640; v_height=360;</script>

我尝试过使用 xpath,并提供了以下代码:

$fid = $xpath->xpath_query("/body/script[contains(local-name(), 'fid')]", 1)->nodeValue;

但运气不好

获得任何帮助

我们必须猜测您输入的大部分文档和代码。我怀疑 body 是那个 HTML 文档的最外层元素。

搜索此 script 元素的有意义的 Xpath 表达式将是

//script[contains(., 'fid')]

这将在文档中的任何地方找到 script 元素,如果它们的文本内容包括 fid。顺便说一句:local-name() returns 上下文节点名称的本地部分 - 这不是您想要的。


编辑

因此,假设以下文档(请不要 post 编码为链接!):

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr">

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>

<body class="contentpane">
    <script type="text/rocketscript">fid="mytarget"; v_width=640; v_height=360;</script>
    <script type="text/rocketscript" data-rocketsrc="http://remotesite.com/js.js"></script>
</body>

</html>

您可以使用

检索 fid 的值
substring-before(substring-after(//*[local-name() = 'script' and contains(.,'fid')],'='),';')

这将 return

"mytarget"

没有引号:

substring-before(substring-after(//*[local-name() = 'script' and contains(.,'fid')],'"'),'"')

警告:在某些环境中,双引号必须以不同方式表示。

您的文档中有一个命名空间,上面的路径表达式忽略那个命名空间。解决这个问题的正确方法是注册这个命名空间并在路径表达式中使用前缀