【问题标题】:Checking if text exists in page source of url检查 url 的页面源中是否存在文本
【发布时间】:2013-12-08 20:05:32
【问题描述】:

我搜索了很多。找到有关如何检查当前网页上是否存在某些字符串的信息。我花了几个小时来找出我的问题: 我正在使用带有 clientdetails 的 XML 文件。

<Name>Name</Name>
<Url>home.example.com</Url>

我用:

window.XMLHttpRequest

获取信息。和

getElementsByTagName("Url")

获取属于客户端名称的Url。

现在我需要使用 javascript 检查每个客户端的此外部 url 的源代码中是否存在某个字符串。那么javascript可能必须在后台打开url的源代码并查找文本字符串?

作为输出,我需要对在 div 中的客户端 url 源代码中存在此文本字符串的每个客户端进行概述。

我希望脚本每 30 分钟执行一次此操作(设置间隔)。

谁能告诉我如何检查外部 url(与 xml 文件中的客户端相关)的源代码中的文本字符串?

谢谢


现在我正在使用它,但它无法检查来自 xml 文件的跨域 url 是否包含源/页面中的文本字符串。有人可以帮忙吗?

<script>
var xhr = new XMLHttpRequest();
xhr.open     ("GET","http://www.example.nl/check/clients.xml",false);
x=xmlDoc.getElementsByTagName("Table1");
i=0
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
// hasMyString = true, if the response contains a given string.
var hasMyString = x[i].getElementsByTagName("Url")[0].childNodes[0].nodeValue).innerText.indexOf('action') !== -1;
  }
  alert("Action Found"); 
}

</script>

【问题讨论】:

  • “外部网址”是否意味着跨域?
  • 是的,确实是跨域的,其实是在

标签: javascript html xml url


【解决方案1】:

您可以使用String#indexOf 检查字符串中是否存在子字符串,因此只需获取XHR 的响应文本并在其中找到您需要的字符串。

var xhr = new XMLHttpRequest();
xhr.open( ... );
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    // hasMyString = true, if the response contains a given string.
    var hasMyString = xhr.innerText.indexOf( ... ) !== -1;
  }
}
xhr.send();

【讨论】:

  • 嗨,对不起。现在我用这个,但它不起作用。我还能做什么?
猜你喜欢
  • 2023-03-25
  • 2012-03-28
  • 2015-11-13
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
相关资源
最近更新 更多