admin管理员组文章数量:1415460
I'm trying to save all the link strings into a text document, but it only saves the last link in the document (in this case Youtube).
I want it to save all the links to the saved txt document, what am I doing wrong?
/
var links = document.querySelectorAll('a');
// Loop through all links
for (var i = 0; i < links.length; i++) {
// Store links in variable
var linksArray = links[i];
// Works fine in console
console.log(linksArray);
}
// Create text document — only saves 1st link in text doc
var textDoc = document.createElement('a');
textDoc.href = 'data:attachment/text,' + encodeURI(linksArray);
textDoc.target = '_blank';
textDoc.download = 'myFile.txt';
textDoc.click();
Can someone help me out here? Thank you! :-)
I'm trying to save all the link strings into a text document, but it only saves the last link in the document (in this case Youtube.).
I want it to save all the links to the saved txt document, what am I doing wrong?
https://jsfiddle/zfL2hzvp/4/
var links = document.querySelectorAll('a');
// Loop through all links
for (var i = 0; i < links.length; i++) {
// Store links in variable
var linksArray = links[i];
// Works fine in console
console.log(linksArray);
}
// Create text document — only saves 1st link in text doc
var textDoc = document.createElement('a');
textDoc.href = 'data:attachment/text,' + encodeURI(linksArray);
textDoc.target = '_blank';
textDoc.download = 'myFile.txt';
textDoc.click();
Can someone help me out here? Thank you! :-)
Share Improve this question asked May 29, 2016 at 13:02 CapaxCapax 2252 silver badges13 bronze badges 1-
1
Your code overwrites the value of
linksArray
on each iteration. That's what an=
assignment does: replace the former value of a variable with a new value. – Pointy Commented May 29, 2016 at 13:03
1 Answer
Reset to default 6(function() {
var links = document.querySelectorAll('a');
var linksArray = [];
// Loop through all links
for (var i = 0; i < links.length; i++) {
// Store links in variable
linksArray.push(links[i]);
// Works fine in console
console.log(linksArray);
}
// Create text document — only saves 1st link in text doc
var textDoc = document.createElement('a');
textDoc.href = 'data:attachment/text,' + encodeURI(linksArray.join('\n'));
textDoc.target = '_blank';
textDoc.download = 'myFile.txt';
textDoc.click();
})();
https://jsfiddle/um4qhsks/1/
本文标签: javascriptSave List of Links from Array to Text File (almost got it working)Stack Overflow
版权声明:本文标题:javascript - Save List of Links from Array to Text File (almost got it working) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745155427a2645142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论