How to add html file into different js folder - javascript

pic
I want to add html file in my js, and this pic is the path in my directory. How can I do?
in my config.js, I widh to include my file as let ADDR_iframe1 = "../templates/switch.html";

Use JavaScript fetch() Method to Load an External HTML File.
Please find the below example for your reference.
function loadHTML() {
fetch('switch.html')
.then(response=> response.text())
.then(text=> document.getElementById('xyz').innerHTML = text);
}

Related

Display contents of file on HTML page

I am looking for a way to display the contents of a directory on an HTML page. I have the following snippet of code:
const fs = require("fs");
let directory_name = "/example";
let filenames = fs.readdirSync(directory_name);
console.log("\nFilenames in directory:");
filenames.forEach((file) => {
console.log("File:", file);
});
This will display the contents of the file in the terminal(console) but I am looking for a way to change the "console.log("File:", file)" line into a statement to send the elements to my HTML page.
Any help would be appreciated.
Using Linux btw
Use the express.js res.send function.

Programmatically download and insert README.md with photos into HTML

I have a personal website written in AngularJS and NodeJS, and in the tech projects section I would like to embed README.md from a project on GitHub my account (the same way github.com) does it. I don't think iframe<> works for md files. So I have tried converting to HTML and PDF.
The second part of the README.md I want looks like this:
In particular, I would like to import the README.md files into my HTML. I have found two ways to do it:
Download the file, and convert to HTML.
var md_to_html = function(src, dest) {
// Get the markdown
var md = fs.readFileSync(src, "utf8");
var converter = new Remarkable();
var html = converter.render(md);
fs.writeFile(dest, html, function(err) {
if (err) {
return console.log(err);
}
console.log(dest + " was saved!");
});
}
Or Download the file, and convert to PDF.
var md_to_pdf = function(src, dest) {
fs.createReadStream(src)
.pipe(markdownpdf())
.pipe(fs.createWriteStream(dest));
}
Both work, however the photo disappears. That is because the dependencies let's say doc/img/photo.jpg does not get downloaded with the README. I have also written scripts to download the dependencies. But neither insert the photo into the README.pdf or README.html.
Has anyone done something like this? Embedded markdown in HTML with photos, or converted them to PDF or HTML with photos? If I could just import it kind of like a PDF with iframe<> that would be ideal, but if not, I will program it myself. But how?

Use javascript variable as file name to download file with href

I have a javascript code that extracts filename as a variable from the current html file. The filename, for example, is "new.html" filename variable is successfully used to append href where I need to open same file strored in another folder. Using the same code, I need to append this variable to a folder path with href tag to download a file with href tag. The file name is extracted from .html (example new) and added to .xls file (example new.xls)
var filename=location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
console.log(filename);
document.getElementById("htag1").href= "Foldername/"+filename;
var object=filename.slice(0,-5);
var xls=".xls";
var xlsfile=object+xls;
$(".xlsfile").text(xlsfile);
console.log(xlsfile);
document.getElementById("d1tag1").href= "Foldername/"+xlsfile;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
#working
#not working
####working download code####
But this is giving me download error with no file getting downloaded
but it is pointing to the right path. Should I use another way for download feature? Before this I had entered the file path statically which seemed to work.
Any help would be appreciated! Thank you!
Try this way, i'm sure now it will work ^^
var filename=location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
console.log(filename);
document.getElementById("htag1").href= "Foldername/"+filename;
var object=filename.split(".")[0];
var xls=".xls";
var xlsfile=object+xls;
$(".xlsfile").text(xlsfile);
console.log(xlsfile);
document.getElementById("d1tag1").href= "Foldername/"+xlsfile;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
#working
#not working
####working download code####

Mustache.js - using partial inside an external template file

I'm using external template file and I want to use a partial inside the template file (.mst file inside another .mst file)
For example I have template.mst file with this code:
{{#comments}}
// Here I want to use another external .mst file
{{/comments}}
So I careated a comment.mst file beside the template.mst file and now I'm trying to use {{>comment}} to call this external template file - full example:
{{#comments}}
{{>comment}}
{{/comments}}
But it doesn't work. I tried to play with it a little bit, I tried to add the file extension {{>comment.mst}} and I tried to add the path {{>temmplates/comment}} and any pther option.
This is the code I use to load the template:
$.get('templates/template.mst', function(template) {
var rendered = Mustache.render(template, postData);
$('.inner-container').prepend(rendered);
});
I guess I missing something, Can someone give me an hint? Thanks!

List the contents of a tar file using javascript

I have a jsp file in which i am writing a function in javascript that takes as argument, a tar file name and returns a list of the file names of the files that the tar file contains.. How can it be done?
Have you tried using UnTar.js ? Its open source js library for that
edit :
providing usage example for clarity
function updateProgressBar(e) { ... update UI element ... }
function displayZipContents(e) { ... display contents of the file ... }
var unzipper = new bitjs.archive.Unzipper(zipFileArrayBuffer);
unzipper.addEventListener("progress", updateProgressBar);
unzipper.addEventListener("finish", displayZipContents);
unzipper.start();
some more info here

Categories