1

I want to insert the following include tag into my webpage using JavaScript. <!--#include virtual='includes/myIncludeFile.htm' -->

I have tried the following but it doesn't work: jQuery("<!--#include virtual='includes/myIncludeFile.htm' -->").appendTo(jQuery("body"));

I have outputted jQuery("<!--#include virtual='includes/myIncludeFile.htm' -->") to the console and it thinks that it is a comment object (see screenshot).

enter image description here

Where am I going wrong and how can it be done?

2
  • 2
    Includes happen on the server. JavaScript runs in the browser. So the answer is no, it can't be done. And yes, the <!-- ... --> syntax denotes an HTML comment, so the browser ignores it; it doesn't understand ASP. Commented Jun 17, 2012 at 15:59
  • well now that isn't good is it. I need to take a different approach. is it possible to have the code I want to insert in a seperate file and grab it from there with JavaScript and then insert it? Commented Jun 17, 2012 at 16:03

1 Answer 1

5

The basic premise of what you're trying to do isn't going to work. The HTML "include tags" you're using are also known as "server-side includes." That is, they are processed on the server before the page is sent to the client.

By the time the JavaScript code is executing on the client, the server is already processing the response. The client-side code can't initiate a server-side include.

One thing you can do from the client-side is use something like jQuery's .load() function to make a request to the server and load the response into a specified element on the page. Something like this:

$('#includeDiv').load('includes/myIncludeFile.htm');

This would dynamically load all of the contents from myIncludeFile.htm into an element of id "includeDiv" on the page.

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.