如何检测网页中是否有'多个html标签

How to detect if there're more than one html tags in web page

本文关键字:多个 标签 html 是否 何检测 检测 网页      更新时间:2023-09-26

我们有一个aspx页面嵌入到一个外部应用程序中,我们最近发现他们在页面中插入了一个额外的html标签,因为一些cookie的原因。他们没有办法删除标签或解决问题。我想如果有任何方法,我可以检测注入额外的html标签从我这边,所以我们可以做一些具体的事情。

最后一页看起来像这样:

<!--Here is the injected stuff-->
<html>
    <body onfocus="document.cookie='blahblahblah'"></body>
</html>
<!--Below is our original markup-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <body>
      Some content
    </body>
</html>

我试过使用jQuery选择器和JavaScript文档。querySelectorAll选择所有的html/body标签,但看起来他们只能找到原始标记中的html/body标签。他们就像

$(function(){
  if($('html').length > 1)
    //Do something
  else if(document.querySelectorAll('html').length > 1)
    //Do something
});

这些都不起作用。当浏览器给出以下警告消息时,它们总是返回length = 1:

HTML1502: Unexpected DOCTYPE。只允许一个DOCTYPE,并且它必须出现在任何元素之前。HTML1513:找到额外的"

"标签。每个文档只能存在一个""标签。

我想知道是否有任何方法,我可以使用JavaScript找到这个额外的标签,或者我可以模拟浏览器做了什么来检测这个额外的标签。

我认为在原生Javascript中不可能做到这一点,因为Html DOM树只能有一个Html标签,这是一种惯例。我认为您可以使用服务器端语言来实现这一点。

就像这样。

$(document).ready(function(){
    $.ajax({
        url: 'yourscript.yourTechno',
        method: 'GET'
    }).done(function(result){
        alert('you have '+result+ 'html tags');
    })
})

在服务器端(例如使用PHP和dom解析器)

$count = 0;
$html = file_get_html('http://www.myWebSite.com/myPageToParse.html');
foreach($html->find('html') as $element){
    $count ++;
}
echo($count);
http://api.jquery.com/jquery.ajax/

http://simplehtmldom.sourceforge.net/