简体   繁体   English

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

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

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. 我们有一个嵌入在外部应用程序中的aspx页面,最近我们发现由于某种cookie的原因,他们在页面中插入了一个额外的html标签。 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. 我想如果有什么办法可以从我这边检测到注入的额外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. 我尝试使用jQuery选择器和JavaScript document.querySelectorAll来选择所有html / body标签,但看起来它们只能在原始标记中找到html / body标签。 They're like 他们就像

$(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. HTML1502:意外的DOCTYPE。 Only one DOCTYPE is allowed and it must occur before any elements. 只允许一个DOCTYPE,并且它必须出现在任何元素之前。 HTML1513: Extra "<html>" tag found. HTML1513:找到额外的“ <html>”标记。 Only one "<html>" tag should exist per document. 每个文档仅应存在一个“ <html>”标记。

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. 感谢帮助。

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. 我认为用原生Javascript不可能做到这一点,因为HTML DOM树只能有一个HTML标签,这是一个约定。 I think you can do this using a serverSide language. 我认为您可以使用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://api.jquery.com/jquery.ajax/

http://simplehtmldom.sourceforge.net/ http://simplehtmldom.sourceforge.net/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查用户不能在HTML网页中输入多个小数点或点 - how to check that user can not input more than one decimal or dot in html web page 如何从具有多个文档 html 的 web 页面中获取元素的选择器? - How get the selector of an element from a web page with more than one document html? 在网页中播放多首歌曲 - Play more than one song in a web page 在HTML页面中添加多个脚本 - adding more than one script into an html page 当页面动态加载超过 20 个音频标签时,如何自动播放一个音频标签? - How to autoplay one audio tag when the page has more than 20 audio tags loaded dynamically? 多次向网页插入自定义 HTML 元素的一个实例 - Insert to web-page one instance of custom HTML element more than once 我需要在一个网页中制作多个模态 - I need to make more than one modal in one web page 如何使用javascript函数getTotal在一个html页面中使用多个函数 - how to use the javascript function getTotal to be used more than one function in one html page 在useEffect中有多个setState,如何避免重新渲染? - more than one setState in useEffect, how to aviod re-render? 将多个模块导入 web 页面的本地方法是什么? - What is the native way to import more than one module into a web page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM