Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从浏览器';使用Javascript查看源代码功能_Javascript_Html_Browser - Fatal编程技术网

从浏览器';使用Javascript查看源代码功能

从浏览器';使用Javascript查看源代码功能,javascript,html,browser,Javascript,Html,Browser,是否可以使用浏览器的功能查看源代码以某种方式将HTML代码转换为Javascript字符串: 我正在尝试使用这样的代码,但我遇到了未定义文档的错误: document.getElementsByTagName('html')[0].innerHTML; 我知道跨域请求是不可能的,除非使用某种黑客,但这看起来容易得多,尽管获取代码非常困难。我不想访问网站来阻止图像和CSS加载。除非其他域与您合作,否则您无法从其他域读取数据 如果您确实有这种合作,您只需发送一个CORS AJAX请求。这是在fir

是否可以使用浏览器的功能查看源代码以某种方式将HTML代码转换为Javascript字符串:

我正在尝试使用这样的代码,但我遇到了未定义文档的错误:

document.getElementsByTagName('html')[0].innerHTML;

我知道跨域请求是不可能的,除非使用某种黑客,但这看起来容易得多,尽管获取代码非常困难。我不想访问网站来阻止图像和CSS加载。

除非其他域与您合作,否则您无法从其他域读取数据


如果您确实有这种合作,您只需发送一个CORS AJAX请求。

这是在firefox中实现的一种方法。其他任何地方都不行。 为了简单起见,我使用了alert()和sync“ajax”,但对于任何ajax库,异步版本都是微不足道的

主要的事情是恢复firefox的漂亮视图源html,它指示行号、html错误,并将html部分(如属性和内容)标记为语义包装。这是我知道的唯一一种在没有互联网连接的情况下在浏览器中验证html的方法

// sync url fetcher function:
function IO(a){var b=new XMLHttpRequest;b.open("GET",a,!1);b.send();return b.responseText}

// create a new iframe to show the source code:
var fr=document.createElement("iframe");

// when it loads, let's view it using a simple alert()
fr.onload=function(){
  alert(win.document.documentElement.outerHTML);
  document.body.removeChild(fr);
};

// now add the frame into the document:
document.body.appendChild(fr);

// now assign the view-source url to the frame to trigger it's onload()
url= "/"; //just use site's home page for this demo
fr.src="view-source:data:text/html,"+escape( IO( url ) );

哦,当然,这只适用于您域中的URL或使用cors设置的URL。

适用于Chrome和Firefox。狩猎旅行。未经测试

document.querySelector('html').innerHTML
*我认为你的错误来自其他地方。这一说法虽然笨拙,但完全正确


如果您遇到未定义文档的错误,那么您是在文档对象准备就绪(您正在等待DOMREADY还是load?)之前执行此操作,或者在DOM接口(web worker?)之外执行此操作。

在firefox中是,在chrome和IE中否。ex::window.open(“查看源代码:data:text/html,+escape(document.body.outerHTML))@丹达维斯,你能说得更准确些吗?我仍然收到关于文档未定义的错误显示产生错误的代码,我可以冒险猜测一下。window.location.href='查看源:';之后我尝试的任何东西都会导致错误,只需要将显示的代码复制到JS stringdocument中,但没有定义!不要对试图帮助你的人急躁。我正在使用Imacros来运行此脚本。这是IOPS产品吗?是的,我正在尝试使用JS旁边的Imacros来获取HTML,没有帮助。我需要在以后的脚本中使用IMacros,所以我在里面使用JS
document.querySelector('html').innerHTML