473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using <!--#include file> dynamically

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
3 3331
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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Colin Mc Mahon | last post by:
Hi, I am trying to re-write an old url management class I use, and am having problems using the Execute method to create vars. I think i'm missing something simple, as I have used this method...
1
by: DC | last post by:
Hi, we are dynamically adding webcontrols in a page like so: Type t = Type.GetType("controls.namespace.control, controls.namespace", false, true); Control c =...
3
by: Nathan Sokalski | last post by:
I am using the AddHandler statement to add a CheckedChanged event handler to a series of RadioButtons that I create in a loop. However, the handler is not being called for a reason I cannot...
12
by: nat | last post by:
Hi all, I was going to post this in the AJAX forum, but apparently someone posted an AJAX/C# question in there today and was told to try this forum instead. So here goes: I'm trying to figure...
1
by: John | last post by:
We have a very large site which uses Page inheritence. All pages inherit from a few base classes, which in turn inherit from Page. The purpose of this is to centrally control header and footer...
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
0
by: =?Utf-8?B?TWlrZQ==?= | last post by:
I am using a sitemap (ASP.net 2.0) along with a treeview control to create the navigation for my website. I want to be able to append to url in the site map a sessionID that I use to store member...
5
by: mrtr33 | last post by:
I have a class table where I want to access the names of courses and the description of each course. The names of the courses go in a drop down list. Based on the user selection, the description...
2
by: kasem | last post by:
Hi... I need to change the background color of my webpage using xml file,so I can create an admin page where i give the admin options of different colors just by pressing a button....Do I have to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.