473,804 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="javas cript">
function checkFileToIncl ude(){
if (location.searc h == "?file1"){
return "<!--#include file='file1.htm l'-->"
}else{
return "<!--#include file='file2.htm l'-->"
}
}
</script>

<html>
..
..
<a href="www.mydom ain.com/test?file1">Inc lude File 1</a>
<a href="www.mydom ain.com/test?file2">Inc lude File 2</a>
..
..
<td>
document.write( checkFileToIncl ude())
</td>

..
..
..
</html>
Jul 20 '05 #1
3 3354
On 27 May 2004 05:35:09 -0700, ar********@hotm ail.com (Adam) wrote:
Hi All,

Is it possible to use includes dynamically in html.
[snip]
<script language="javas cript">
function checkFileToIncl ude(){
if (location.searc h == "?file1"){
return "<!--#include file='file1.htm l'-->"
}else{
return "<!--#include file='file2.htm l'-->"
}
}
</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 checkFileToIncl ude(){
if (location.searc h == "?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********@hot mail.com> wrote in message
news:a8******** *************** ***@posting.goo gle.com...
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="javas cript">
function checkFileToIncl ude(){
if (location.searc h == "?file1"){
return "<!--#include file='file1.htm l'-->"
}else{
return "<!--#include file='file2.htm l'-->"
}
}
</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********@hot mail.com> wrote in message
news:a8******** *************** ***@posting.goo gle.com...
: 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="javas cript">
: function checkFileToIncl ude(){
: if (location.searc h == "?file1"){
: return "<!--#include file='file1.htm l'-->"
: }else{
: return "<!--#include file='file2.htm l'-->"
: }
: }
: </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
1503
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 several times to perform this kind of action, maybe it just needs some fresh eyes :) The code i'm using is as follows: Class cURL ' *--- --------------------------------------------------* Private i_ItemCount
1
1491
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 = (Control)Activator.CreateInstance(t, true); this.Controls.Add(c); Now we would like to cache this webcontrol.
3
1664
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 determine. What is the problem? Here is my code: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load currpoem = Server.UrlDecode(Request.QueryString("poem")) Dim ratinglabels As New TableRow Dim...
12
3494
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 out whether AJAX will work with dynamically created controls in C#.NET. I have been able to get non-dynamically created controls to work, but so far none of my dynamic controls or events linked to them are able to work with AJAX. Does anyone know...
1
1015
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 content, as well as a custom permissions-system. All <html>, <head>, and <body> tags are generated in the parent class using writer.Write (in Render), then base.Render is called to generate the content of the subclass (each page). Unfortunately...
9
1868
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, extra_information): super(MetaThing, cls).__init__(name, bases, dict)
0
1262
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 information. Example: Sitemap in web.config contains "home.aspx". When the treeview is rendered, I want the hyperlink to be "home.aspx?sessionID=8484". The sessionID is available in the codebehind and is unique for each user.
5
2722
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 shows up as text beneath the drop down menu. I have the following code: $queryReq="SELECT name, description FROM class"; $resultReq = @mysql_query ($queryReq); echo "<br><form action=index.php name='trouble' method='POST'><select name='class' >";...
2
4358
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 create a different pages with different background colors and use xml with url to each of these pages..Any suggestions are welcome...Thanks
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10350
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.