By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,847 Members | 2,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,847 IT Pros & Developers. It's quick & easy.

Using <!--#include file> dynamically

P: n/a
Hi All,

Is it possible to use includes dynamically in html.

Hopefully the following example will give an idea what I am rying to do.

Thanks in advance
Adam

<script language="javascript">
function checkFileToInclude(){
if (location.search == "?file1"){
return "<!--#include file='file1.html'-->"
}else{
return "<!--#include file='file2.html'-->"
}
}
</script>

<html>
..
..
<a href="www.mydomain.com/test?file1">Include File 1</a>
<a href="www.mydomain.com/test?file2">Include File 2</a>
..
..
<td>
document.write(checkFileToInclude())
</td>

..
..
..
</html>
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 27 May 2004 05:35:09 -0700, ar********@hotmail.com (Adam) wrote:
Hi All,

Is it possible to use includes dynamically in html.
[snip]
<script language="javascript">
function checkFileToInclude(){
if (location.search == "?file1"){
return "<!--#include file='file1.html'-->"
}else{
return "<!--#include file='file2.html'-->"
}
}
</script>

[snip]
That will work, but it won't do what I think you're expecting.

Assuming file1.html contains the string "Blah" and file2.html contains
the string "Foo", you'll get this function:

function checkFileToInclude(){
if (location.search == "?file1"){
return "Blah"
}else{
return "Foo"
}
}

In other words, both files will be included in the script regardless
of whether that condition matches, because the JavaScript code runs in
the browser.

Also, it'll break as soon as you put an unescaped quote symbol in your
included file since it'll terminate the string.

It'd be far more sensible to do the check on the query string on the
server, perhaps using Apache's more advanced SSI or some other
scripting language and only send the file you wish to be used for that
particular request.

-Claire
Jul 20 '05 #2

P: n/a

"Adam" <ar********@hotmail.com> wrote in message
news:a8**************************@posting.google.c om...
Hi All,

Is it possible to use includes dynamically in html.

Hopefully the following example will give an idea what I am rying to do.

Thanks in advance
Adam

<script language="javascript">
function checkFileToInclude(){
if (location.search == "?file1"){
return "<!--#include file='file1.html'-->"
}else{
return "<!--#include file='file2.html'-->"
}
}
</script>

<html>


You have to understand the order in which the steps involved in serving and
receiving a web page are carried out, and where each of them is carried out.
The INCLUDE directives are expanded on the *server*. By the time the code is
sent to the client, which is where your script is executed, there *are* no
more INCLUDE directives; they have been replaced by the contents of
file1.html and file2.html, respectively.

Jul 20 '05 #3

P: n/a
"Adam" <ar********@hotmail.com> wrote in message
news:a8**************************@posting.google.c om...
: Hi All,
:
: Is it possible to use includes dynamically in html.
:
: Hopefully the following example will give an idea what I am rying to do.
:
: Thanks in advance
: Adam
:
: <script language="javascript">
: function checkFileToInclude(){
: if (location.search == "?file1"){
: return "<!--#include file='file1.html'-->"
: }else{
: return "<!--#include file='file2.html'-->"
: }
: }
: </script>
:
As Claire pointed out, the processing order is wrong since #include is processed
as the page is sent to the browser, then the JS is processed by the browser.
This means your HTML may be malformed.

You'd be better off using server-side processing such as PHP/Perl/CGI. Our
hosting service provides similar processing using custom tags. In your case
I can use a @topic tag to conditionally include content base on query string
values. The main difference is I don't write the IF..ELSE statement.

--
Long
www.webcharm.ca - Integrated content management web hosting
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.