Javascript 获取iframe的源代码

Javascript 获取iframe的源代码,javascript,html,iframe,Javascript,Html,Iframe,是否仍然可以获取iframe加载的页面的源代码?我不想更改任何代码,我只想阅读它。我还需要能够使用javascript/html实现这一点。请参阅基于Mozilla的浏览器如何处理它。您最喜欢的JavaScript库中应该有一些抽象,它们将处理IE、WebKit、Gecko等DOM之间不可避免的命名差异。使用JQuery:(仅当域匹配时) 例如: $(iframe).contents().find('body').html(); 如果不匹配,您可以再次加载它(因为它可能已存储在客户机中,因此可

是否仍然可以获取iframe加载的页面的源代码?我不想更改任何代码,我只想阅读它。我还需要能够使用javascript/html实现这一点。

请参阅基于Mozilla的浏览器如何处理它。您最喜欢的JavaScript库中应该有一些抽象,它们将处理IE、WebKit、Gecko等DOM之间不可避免的命名差异。

使用JQuery:(仅当域匹配时)

例如:

$(iframe).contents().find('body').html();
如果不匹配,您可以再次加载它(因为它可能已存储在客户机中,因此可能不会再次转到服务器):

适用于Firefox、Chrome、Opera、IE9测试版

<!DOCTYPE html>
<html>
<body>

//this is iframe I ll look for its source
<iframe id="frmin" src="http://www.w3schools.com"></iframe>

<p>Click the button to see the source.</p>
//this is display I will display Iframe's source here
<div id="srcout"></div>

//show source upon click event
<button onclick="myFunction()">Show it</button>

<script>
function myFunction() {
//get Iframe element ready for javascript manipulation
var frm = document.getElementById("frmin");
//get display element ready for javascript manipulation
var dsp = document.getElementById("srcout");

//find Iframe's HTML (root) element, [0] because I'm using getElementsByTagName which returns node list so I have to chose the 1st one, and put its outer content (i.e. with outer tags) to display, i.e. dsp.innerText. I use innerText because I want a text output not formatted as html.
dsp.innerText = frm.contentDocument.getElementsByTagName('html')[0].outerHTML;

}
</script>

</body> 
</html>

//这是iframe,我将查找其来源
单击按钮查看源代码

//这是display,我将在这里显示Iframe的源代码 //单击事件时显示源 表现出来 函数myFunction(){ //为javascript操作准备好Iframe元素 var frm=document.getElementById(“frmin”); //为javascript操作准备好显示元素 var dsp=document.getElementById(“srcout”); //查找Iframe的HTML(根)元素[0],因为我使用的是getElementsByTagName,它返回节点列表,所以我必须选择第一个元素,并将其外部内容(即带有外部标记)显示出来,即dsp.innerText。我使用innerText是因为我希望文本输出的格式不是HTML。 dsp.innerText=frm.contentDocument.getElementsByTagName('html')[0].outerHTML; }
我知道这看起来很复杂,但我为您提供了一种100%防弹的方式,可以在页面上查看源代码。 我不知道如何在iframes中显示它,但有另一种方式来查看源代码,它比iframes好得多,这就是如何查看源代码

重要的是JavaScript和HTML与此完全相同

CSS:
部分:

<style type="text/css" >
.button
    {
    background:#000cff;
    padding:2px 10px;
    color:white;
    text-decoration:none;
    -moz-border-radius:10px;
    -webkit-border-radius:10px;
    border-radius:10px;
    cursor: pointer;
    font-weight: bold;
    color: white;
    display: inline-block;
    box-shadow: 0 0 3px gray;
    margin: 0px;
    font: bold 11px Arial, Sans-Serif;
    }

#source-code
    {
    display:none;
    position:absolute;
    top:-24px;
    left:0;
    width:100%;
    height:414px;
    background:rgba(255,255,255,0.0);
    }

#source-code:target { display: block; }
#source-code pre
    {
    padding:20px;
    font:14px/1.6 Monaco, Courier, MonoSpace;
    margin:50px auto;
    background:rgba(0,0,0,0.8);
    color:white;
    width:80%;
    height:80%;
    overflow:auto;
    }

#source-code pre a,
#source-code pre a span
    {
    text-decoration:none;
    color:#00ccff !important;
    }

#x
    {
    position:absolute;
    top:30px;
    left:10%;
    margin-left:-41px;
    }

.control-copytextarea
    {
    position:absolute;
    top:-3px;
    left:20px;
    cursor: pointer;
    font-weight: bold;
    padding:3px 10px;
    border-radius: 5px 5px 0 0;
    background: darkred;
    color: white;
    display: inline-block;
    box-shadow: 0 0 3px gray;
    margin: 0px;
    font: bold 10px Arial, Sans-Serif;
    }
</style>
<a class="button" href="#source-code" id="view-source">Show the Source Code for this page</a>
<div id="source-code" align="left">
    <a href="#" id="x"><input type="button" alt="close" class="control-copytextarea" value=" Close Source Code Viewing " /></a>
</div>

按钮
{
背景:#000cff;
填充:2x10px;
颜色:白色;
文字装饰:无;
-moz边界半径:10px;
-webkit边界半径:10px;
边界半径:10px;
光标:指针;
字体大小:粗体;
颜色:白色;
显示:内联块;
长方体阴影:0 3px灰色;
边际:0px;
字体:粗体11px Arial,无衬线;
}
#源代码
{
显示:无;
位置:绝对位置;
顶部:-24px;
左:0;
宽度:100%;
高度:414px;
背景:rgba(255255,0.0);
}
#源代码:目标{display:block;}
#源代码预处理
{
填充:20px;
字体:14px/1.6摩纳哥,信使,MonoSpace;
保证金:50px自动;
背景:rgba(0,0,0,0.8);
颜色:白色;
宽度:80%;
身高:80%;
溢出:自动;
}
#源代码预处理,
#span前的源代码
{
文字装饰:无;
颜色:#00ccff!重要;
}
#x
{
位置:绝对位置;
顶部:30px;
左:10%;
左边距:-41px;
}
.控制copytextarea
{
位置:绝对位置;
顶部:-3px;
左:20px;
光标:指针;
字体大小:粗体;
填充:3x10px;
边界半径:5px5px0;
背景:黑暗;
颜色:白色;
显示:内联块;
长方体阴影:0 3px灰色;
边际:0px;
字体:粗体10px Arial,无衬线;
}
JavaScript:

<script languade="JavaScript">
        $(function() {
            $("<pre />", {
                "html":   '&lt;!DOCTYPE html>\n&lt;html>\n' + 
                        $("html")
                            .html()
                            .replace(/[<>]/g, function(m) { return {'<':'&lt;','>':'&gt;'}[m]})
                            .replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>') + 
                        '\n&lt;/html>',
                "class": "prettyprint"
            }).appendTo("#source-code");

            prettyPrint();
        });
</script>

<script languade="JavaScript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script languade="JavaScript">
        var pageTracker = _gat._getTracker("UA-68528-29");
        pageTracker._initData();
        pageTracker._trackPageview();
</script>
<html>
  <head><title>View your Source Code</title>

<style type="text/css" >
.button
    {
    background:#000cff;
    padding:2px 10px;
    color:white;
    text-decoration:none;
    -moz-border-radius:10px;
    -webkit-border-radius:10px;
    border-radius:10px;
    cursor: pointer;
    font-weight: bold;
    color: white;
    display: inline-block;
    box-shadow: 0 0 3px gray;
    margin: 0px;
    font: bold 11px Arial, Sans-Serif;
    }

#source-code
    {
    display:none;
    position:absolute;
    top:-24px;
    left:0;
    width:100%;
    height:414px;
    background:rgba(255,255,255,0.0);
    }

#source-code:target { display: block; }
#source-code pre
    {
    padding:20px;
    font:14px/1.6 Monaco, Courier, MonoSpace;
    margin:50px auto;
    background:rgba(0,0,0,0.8);
    color:white;
    width:80%;
    height:80%;
    overflow:auto;
    }

#source-code pre a,
#source-code pre a span
    {
    text-decoration:none;
    color:#00ccff !important;
    }

#x
    {
    position:absolute;
    top:30px;
    left:10%;
    margin-left:-41px;
    }

.control-copytextarea
    {
    position:absolute;
    top:-3px;
    left:20px;
    cursor: pointer;
    font-weight: bold;
    padding:3px 10px;
    border-radius: 5px 5px 0 0;
    background: darkred;
    color: white;
    display: inline-block;
    box-shadow: 0 0 3px gray;
    margin: 0px;
    font: bold 10px Arial, Sans-Serif;
    }
</style>

<script languade="JavaScript">
        $(function() {
            $("<pre />", {
                "html":   '&lt;!DOCTYPE html>\n&lt;html>\n' + 
                        $("html")
                            .html()
                            .replace(/[<>]/g, function(m) { return {'<':'&lt;','>':'&gt;'}[m]})
                            .replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>') + 
                        '\n&lt;/html>',
                "class": "prettyprint"
            }).appendTo("#source-code");

            prettyPrint();
        });
</script>

<script languade="JavaScript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script languade="JavaScript">
        var pageTracker = _gat._getTracker("UA-68528-29");
        pageTracker._initData();
        pageTracker._trackPageview();
</script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="http://css-tricks.com/examples/ViewSourceButton/prettify/prettify.js"></script>

  </head>
<body>



<a class="button" href="#source-code" id="view-source">Show the Source Code for this page</a>
<div id="source-code" align="left">
    <a href="#" id="x"><input type="button" alt="close" class="control-copytextarea" value=" Close Source Code Viewing " /></a>
</div>


  </body>
</html>

$(函数(){
$("", {
“html”:“!DOCTYPE html>\nhtml>\n”+
$(“html”)
.html()
.replace(/[]/g,函数(m){return{'':''}[m]})
.replace(/((ftp | http | https):\/\/(\w+:{0,1}\w*@)(\S+)(:[0-9]+)(\/\\/([\w!:?+=&%@!\-\/]))/gi+
“\n/html>”,
“类”:“预打印”
}).appendTo(“#源代码”);
预打印();
});
var gaJsHost=((“https:==document.location.protocol)?”https://ssl." : "http://www.");
write(unescape(“%3Cscript src=”+gaJsHost+“google analytics.com/ga.js”type='text/javascript'%3E%3C/script%3E”);
var pageTracker=_gat._getTracker(“UA-68528-29”);
页面跟踪器。_initData();
页面跟踪器。_trackPageview();
注意: 您不需要在线使用这些JavaScript代码,但为了让它在所有浏览器上都能工作,建议您也使用它们

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://css-tricks.com/examples/ViewSourceButton/prettify/prettify.js"></script>

HTML:
部分:

<style type="text/css" >
.button
    {
    background:#000cff;
    padding:2px 10px;
    color:white;
    text-decoration:none;
    -moz-border-radius:10px;
    -webkit-border-radius:10px;
    border-radius:10px;
    cursor: pointer;
    font-weight: bold;
    color: white;
    display: inline-block;
    box-shadow: 0 0 3px gray;
    margin: 0px;
    font: bold 11px Arial, Sans-Serif;
    }

#source-code
    {
    display:none;
    position:absolute;
    top:-24px;
    left:0;
    width:100%;
    height:414px;
    background:rgba(255,255,255,0.0);
    }

#source-code:target { display: block; }
#source-code pre
    {
    padding:20px;
    font:14px/1.6 Monaco, Courier, MonoSpace;
    margin:50px auto;
    background:rgba(0,0,0,0.8);
    color:white;
    width:80%;
    height:80%;
    overflow:auto;
    }

#source-code pre a,
#source-code pre a span
    {
    text-decoration:none;
    color:#00ccff !important;
    }

#x
    {
    position:absolute;
    top:30px;
    left:10%;
    margin-left:-41px;
    }

.control-copytextarea
    {
    position:absolute;
    top:-3px;
    left:20px;
    cursor: pointer;
    font-weight: bold;
    padding:3px 10px;
    border-radius: 5px 5px 0 0;
    background: darkred;
    color: white;
    display: inline-block;
    box-shadow: 0 0 3px gray;
    margin: 0px;
    font: bold 10px Arial, Sans-Serif;
    }
</style>
<a class="button" href="#source-code" id="view-source">Show the Source Code for this page</a>
<div id="source-code" align="left">
    <a href="#" id="x"><input type="button" alt="close" class="control-copytextarea" value=" Close Source Code Viewing " /></a>
</div>

注意:您可以直接将代码复制并粘贴到您的网页上,它将按原样工作

完整示例:

<script languade="JavaScript">
        $(function() {
            $("<pre />", {
                "html":   '&lt;!DOCTYPE html>\n&lt;html>\n' + 
                        $("html")
                            .html()
                            .replace(/[<>]/g, function(m) { return {'<':'&lt;','>':'&gt;'}[m]})
                            .replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>') + 
                        '\n&lt;/html>',
                "class": "prettyprint"
            }).appendTo("#source-code");

            prettyPrint();
        });
</script>

<script languade="JavaScript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script languade="JavaScript">
        var pageTracker = _gat._getTracker("UA-68528-29");
        pageTracker._initData();
        pageTracker._trackPageview();
</script>
<html>
  <head><title>View your Source Code</title>

<style type="text/css" >
.button
    {
    background:#000cff;
    padding:2px 10px;
    color:white;
    text-decoration:none;
    -moz-border-radius:10px;
    -webkit-border-radius:10px;
    border-radius:10px;
    cursor: pointer;
    font-weight: bold;
    color: white;
    display: inline-block;
    box-shadow: 0 0 3px gray;
    margin: 0px;
    font: bold 11px Arial, Sans-Serif;
    }

#source-code
    {
    display:none;
    position:absolute;
    top:-24px;
    left:0;
    width:100%;
    height:414px;
    background:rgba(255,255,255,0.0);
    }

#source-code:target { display: block; }
#source-code pre
    {
    padding:20px;
    font:14px/1.6 Monaco, Courier, MonoSpace;
    margin:50px auto;
    background:rgba(0,0,0,0.8);
    color:white;
    width:80%;
    height:80%;
    overflow:auto;
    }

#source-code pre a,
#source-code pre a span
    {
    text-decoration:none;
    color:#00ccff !important;
    }

#x
    {
    position:absolute;
    top:30px;
    left:10%;
    margin-left:-41px;
    }

.control-copytextarea
    {
    position:absolute;
    top:-3px;
    left:20px;
    cursor: pointer;
    font-weight: bold;
    padding:3px 10px;
    border-radius: 5px 5px 0 0;
    background: darkred;
    color: white;
    display: inline-block;
    box-shadow: 0 0 3px gray;
    margin: 0px;
    font: bold 10px Arial, Sans-Serif;
    }
</style>

<script languade="JavaScript">
        $(function() {
            $("<pre />", {
                "html":   '&lt;!DOCTYPE html>\n&lt;html>\n' + 
                        $("html")
                            .html()
                            .replace(/[<>]/g, function(m) { return {'<':'&lt;','>':'&gt;'}[m]})
                            .replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>') + 
                        '\n&lt;/html>',
                "class": "prettyprint"
            }).appendTo("#source-code");

            prettyPrint();
        });
</script>

<script languade="JavaScript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script languade="JavaScript">
        var pageTracker = _gat._getTracker("UA-68528-29");
        pageTracker._initData();
        pageTracker._trackPageview();
</script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="http://css-tricks.com/examples/ViewSourceButton/prettify/prettify.js"></script>

  </head>
<body>



<a class="button" href="#source-code" id="view-source">Show the Source Code for this page</a>
<div id="source-code" align="left">
    <a href="#" id="x"><input type="button" alt="close" class="control-copytextarea" value=" Close Source Code Viewing " /></a>
</div>


  </body>
</html>

查看您的源代码
按钮
{
背景:#000cff;
填充:2x10px;
颜色:白色;
文字装饰:无;
-moz边界半径:10px;
-webkit边界半径:10px;
边界半径:10px;
光标:指针;
字体大小:粗体;
颜色:白色;
显示:内联块;
长方体阴影:0 3px灰色;
边际:0px;
字体:粗体11px Arial,无衬线;
}
#源代码
{
显示:无;
位置:绝对位置;
顶部:-24px;
左:0;
宽度:100%;
高度:414px;
背景:rgba(255255,0.0);
}
#源代码:目标{display:block;}
#源代码预处理
{
填充:20px;
字体:14px/1.6摩纳哥,信使,MonoSpace;
保证金:50px自动;
背景:rgba(0,0,0,0.8);
颜色:白色;
宽度:80%;
身高:80%;
溢出:自动;
}
#源代码预处理,
#span前的源代码
{
文字装饰:无;
颜色:#00ccff!重要;
}
#x
{
位置:绝对位置;
顶部:30px;
左:10%;
左边距:-41px;
}
.控制copytextarea
{
位置:绝对位置;
顶部:-3px;
左:20px;
光标:指针;
字体大小:粗体;
填充:3x10px;
边界半径:5px5px0;
背景:黑暗;
颜色:白色;
显示:内联块;
长方体阴影:0 3px灰色;
边际:0px;
字体:粗体10px Arial,无衬线;