1

Is there any simple way to add or link external javascript file into all .html files in directory by java script.

I have a folder called src in that there are n-number of .html files are available. I need to provide <script src="event_listener.js"></script> manually in each .html files, I'm looking for automate this step by javascript. So far I tried by reading a file like below,

$.get(path_of_the_file, function( my_var ) {
    console.log(my_var)
}, 'html');

But I'm having cross-domain issue. So i'm unable to achieve the same.

I'm new to javascript. So I don't have an idea to solve this issue. If I have this problem in python I could try something like below,

import os
file_path=os.listdir('path_of_the_files')
for file_ in file_path:
   if file_.endswith('.html'):
      fo=open(file_path+'/'+file_,'r')
      i=0
      lines=fo.readlines()
      for line in lines:
         if line.strip().startwith("</html>"):
              el=i
              break
         i+=1
      lines.insert(["<script src="event_listener.js"></script>"],i)
      fo.close()
      fo=open(file_path+'/'+file_,'w')
      fo.writelines( lines )

At the worst case I could try with python.

But I hope this could be solved by without moving to python.

Thanks in Advance.

11
  • i think you have to do it manually Commented Jul 5, 2018 at 10:57
  • @AdeshKumar - Thanks for the comment. But I have nearly 20 to 50 html files and it's dynamic. So it's a critical part to do it by manually. Commented Jul 5, 2018 at 10:58
  • With only HTML, you need to reference your JavaScript file. There is no workaround. Commented Jul 5, 2018 at 11:01
  • As I know you have to do it maually. or you can go to any other language for doing so as javascript doesn't provide it. Commented Jul 5, 2018 at 11:02
  • @rojadesign - I can use javascript also :). Is there any way I can achieve this by javascript Commented Jul 5, 2018 at 11:03

1 Answer 1

1

You can use nodeJS filesystem API to edit all files in the folder. https://nodejs.org/api/fs.html

Here's how you read all files in a folder and apply a onFileContent function to each file:

var fs = require('fs');

function readFiles(dirname, onFileContent, onError) {
  fs.readdir(dirname, function(err, filenames) {
    if (err) {
      onError(err);
      return;
    }
    filenames.forEach(function(filename) {
      fs.readFile(dirname + filename, 'utf-8', function(err, content) {
        if (err) {
          onError(err);
          return;
        }
        onFileContent(filename, content);
      });
    });
  });
}

And this is how you write to a single file:

fs.writeFile('filename.txt', newValue, 'utf-8', function (err) {
  if (err) throw err;
});

Hope this helps you. Don't forget that this is Nodejs, it's not a browser script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.