Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.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 - Fatal编程技术网

Javascript 如何下载当前网页

Javascript 如何下载当前网页,javascript,html,Javascript,Html,我想添加一个按钮来下载网页的html源代码, 我能够做到这一点,但问题是,当我添加本地主机url时,它可以工作,但当我添加实时url时,它打开的是url,而不是下载它。到目前为止我试过这个 <a download href="http://localhost/test-new/">Download Source Code</a> <button onclick="clicker()">Click me</button> <scri

我想添加一个按钮来下载网页的html源代码, 我能够做到这一点,但问题是,当我添加本地主机url时,它可以工作,但当我添加实时url时,它打开的是url,而不是下载它。到目前为止我试过这个

 <a download  href="http://localhost/test-new/">Download Source Code</a>
<button onclick="clicker()">Click me</button>



<script type="text/javascript">

function clicker() {
    var anchorTag = document.createElement('a');
    anchorTag.href = "http://localhost/test-new/";
    anchorTag.download = "download";
    anchorTag.click();


    var element = document.getElementById('divContainer');
    element.appendChild(anchorTag);
}

</script>

我也试过这个

 <a download  href="http://localhost/test-new/">Download Source Code</a>
<button onclick="clicker()">Click me</button>



<script type="text/javascript">

function clicker() {
    var anchorTag = document.createElement('a');
    anchorTag.href = "http://localhost/test-new/";
    anchorTag.download = "download";
    anchorTag.click();


    var element = document.getElementById('divContainer');
    element.appendChild(anchorTag);
}

</script>
点击我
函数点击器(){
var anchorTag=document.createElement('a');
anchorTag.href=”http://localhost/test-new/";
anchorTag.download=“下载”;
anchorTag.click();
var元素=document.getElementById('divContainer');
元素.appendChild(anchorTag);
}
这两种方法都在localhost中工作,但不适用于实时url。请帮帮我你可以用

document.querySelectorAll('*')
获取网页的源代码的步骤

console.log(document.querySelectorAll('*')
您可以使用

document.querySelectorAll('*')
获取网页的源代码的步骤


console.log(document.querySelectorAll('*')
此代码应该可以工作

但是有一个主要限制,可以阻止您下载其他页面-。如果此策略不允许,则无法从其他域获取数据。现代浏览器出于安全原因对此进行了限制

但是,如果您下载当前页面,则不会出现任何问题。有关错误,请参见Dev Tools控制台输出:

从源站获取“URL\u HERE”的访问已被CORS阻止…

const btn=document.querySelector('.download btn');
btn.addEventListener('click',(e)=>clicker(e.currentTarget.dataset.url),false);
功能点击器(url){
获取(url)
。然后(res=>res.text())
.then(html=>downloadAsFile('My file name.txt',html));
}
函数downloadAsFile(名称、文本){
const link=createDownloadableLink(名称、文本);
const clickEvent=new MouseEvent('click');
link.dispatchEvent(点击事件);
}
函数createDownloadableLink(文件名、内容){
让link=document.createElement(“a”);
link.download=文件名;
link.href=`data:application/octet-stream,${content}`;
返回链接;
}

如果CORS允许,请单击“我”
此代码应该有效

但是有一个主要限制,可以阻止您下载其他页面-。如果此策略不允许,则无法从其他域获取数据。现代浏览器出于安全原因对此进行了限制

但是,如果您下载当前页面,则不会出现任何问题。有关错误,请参见Dev Tools控制台输出:

从源站获取“URL\u HERE”的访问已被CORS阻止…

const btn=document.querySelector('.download btn');
btn.addEventListener('click',(e)=>clicker(e.currentTarget.dataset.url),false);
功能点击器(url){
获取(url)
。然后(res=>res.text())
.then(html=>downloadAsFile('My file name.txt',html));
}
函数downloadAsFile(名称、文本){
const link=createDownloadableLink(名称、文本);
const clickEvent=new MouseEvent('click');
link.dispatchEvent(点击事件);
}
函数createDownloadableLink(文件名、内容){
让link=document.createElement(“a”);
link.download=文件名;
link.href=`data:application/octet-stream,${content}`;
返回链接;
}

如果CORS允许,请单击“我”
它对我不起作用发送
ajax请求
并编写跨源文档对我不起作用尝试发送
ajax请求
并编写跨源文档