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

时间:2022-11-26 14:08:29

We have an aspx page that is embed in an external application, and we recently found they inserted an extra html tag in the page for some cookie reason. There is no way they can remove the tag or fix the problem. I would think if there's any way that I can detect the injected extra html tag from my side so we can do something specific for it.

我们有一个嵌入在外部应用程序中的aspx页面,我们最近发现他们在页面中插入了一个额外的html标记,以获得一些cookie原因。他们无法删除标签或解决问题。我想如果有任何方法可以检测到我身边注入的额外html标签,那么我们可以为它做一些特定的事情。

The final page would look like this:

最后一页看起来像这样:

<!--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>

I have tried use jQuery selectors and JavaScript document.querySelectorAll to select all the html/body tags but look like they can only find the html/body tag in original markup. They're like

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

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

None of these worked. They always return length = 1 while the browser is giving the following warning message:

这些都没有奏效。当浏览器发出以下警告消息时,它们总是返回length = 1:

HTML1502: Unexpected DOCTYPE. Only one DOCTYPE is allowed and it must occur before any elements. HTML1513: Extra "<html>" tag found. Only one "<html>" tag should exist per document.

HTML1502:意外的DOCTYPE。只允许一个DOCTYPE,它必须在任何元素之前发生。 HTML1513:找到了额外的“”标记。每个文档只应存在一个“”标记。

I am wondering if there's any way that I can find this extra tag using JavaScript or can I simulate what the browser did to detect this extra tag.

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

Thanks for help.

感谢帮助。

1 个解决方案

#1


0  

I think it's not possible to do that in native Javascript, because the Html DOM Tree can have only one Html tag and it's a convention. I think you can do this using a serverSide language.

我认为在原生Javascript中不可能这样做,因为Html DOM树只能有一个Html标签,这是一个惯例。我想你可以使用serverSide语言来做到这一点。

Something like this.

像这样的东西。

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

On the server side (with PHP and a dom parser for example)

在服务器端(例如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/

#1


0  

I think it's not possible to do that in native Javascript, because the Html DOM Tree can have only one Html tag and it's a convention. I think you can do this using a serverSide language.

我认为在原生Javascript中不可能这样做,因为Html DOM树只能有一个Html标签,这是一个惯例。我想你可以使用serverSide语言来做到这一点。

Something like this.

像这样的东西。

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

On the server side (with PHP and a dom parser for example)

在服务器端(例如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/