0

I have 2 HTML Files.

these 2 HTML files will make be needed to added.

Is there a way in Javascript, Jquery that I can add 2 files and get a single file.

In Jquery I saw append and other functions but my usecase is I want to add 2 Big files

Christian
  • 19,605
  • 3
  • 54
  • 70
The Learner
  • 3,867
  • 14
  • 40
  • 50
  • 4
    javascript is a front end language, sounds like you want to generate a file, which you'll need to do in a server side language such as php. – Christian Nov 22 '12 at 16:12
  • What do you want to do with the concatenated file, where do you need it? Do you want to store it, or do you want to use it in further processing? – Bergi Nov 22 '12 at 16:12
  • You can't handle files with js, without specific addons or plugins in your browser, whatever you are trying to do is better to achieve it from the back end if possible – aleation Nov 22 '12 at 16:13
  • you can make an ajax call to each file and load them into one place in page. Would require 2 ajax calls – charlietfl Nov 22 '12 at 16:13
  • 1
    @Christian: JavaScript can also be a backend-language, and with Node.js you will be able to read two files and print one. – Bergi Nov 22 '12 at 16:13
  • Bergi Thank you . I am looking to use this in express(node)..I am looking how I can add 2 files in expressnode and also how I can add some variables/text to that exsisting htmlpage in node. How to do that? – The Learner Nov 22 '12 at 16:15
  • Haha, I didn't actually think this was referring to node. Incorrect tags. – Christian Nov 22 '12 at 16:16
  • I don't think he knows what he wants ... – SpYk3HH Nov 22 '12 at 16:16
  • I did not mention node in the actual question because I want to know from java script perceptive..for me node is almost like java script.. – The Learner Nov 22 '12 at 16:17
  • node _is_ javascript, but there's a big difference between front-end and back-end javascript. And jQuery is for front end use, and you're not going to be able to generate a file that way. – Christian Nov 22 '12 at 16:17
  • SpYk3HH- please dont come to conclusion :-) I know what I am asking – The Learner Nov 22 '12 at 16:18
  • 1
    Actually [you can use jQuery with node](http://stackoverflow.com/questions/1801160/can-i-use-jquery-with-node-js), learning something every day! – Christian Nov 22 '12 at 16:19
  • ok Thank you. so if I use javascript as backend how do I make it – The Learner Nov 22 '12 at 16:20

1 Answers1

1

I don't have the exact code but you shouldn't need it (jQuery well documented)

  1. Use Ajax to get the HTML data (the code)
  2. Presumably you want to add the content? You'll need to get the data between the body tag (and possibly between the head tag too)
  3. Concatenate those. Remember, the ajax data is a string

Although, this type of thing sounds better for a server side language like PHP. You can make an ajax request for the "builder" PHP file, which will return the two files put together. Plus, if you use templates right, you won't need to do the head/body filtering messy stuff

Example pseudocode

//template.php
<html>
    <head></head>
    <body>
        <?php include 'foo.php'; ?>
    </body>
</html>

//foo.php
My html content
<h1>Hello</h1>

//bar.php
My other page
<h1>About</h1>

//builder.php
<html>
    <head></head>
    <body>
        <?php include 'foo.php'; include 'bar.php'; ?>
    </body>
</html>

Not the best example (very, very simple) but it should give you the idea. Get an ajax request on builder.php, and that's all your content. You could make it even more powerful by creating a builder.php file that takes filename parameters to concatenate (and you should do validation on these files...)

Raekye
  • 5,081
  • 8
  • 49
  • 74