The Learner
The Learner

Reputation: 3917

concatenate 2 files

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

Upvotes: 0

Views: 237

Answers (1)

Raekye
Raekye

Reputation: 5131

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...)

Upvotes: 1

Related Questions