Dynamic include files

A

Adam

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>
 
G

Guido Wesdorp

Adam said:
Hi All,

Is it possible to use includes dynamically in html.
....

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

Lol not unless this script runs on the server... I assume you want to
perform the actions in the #include statements, and those should be
performed on the server side, since the JavaScript is executed on the
client side the server will never know what process.
The funny thing is that this may actually work, but then the other way
round from what you seem to expect: the server will, *before* sending
the result to the client, replace the SSI instructions with the content
of *both* html files and later on the JavaScript picks one of those to
use...

Cheers,

Guido
 
K

kaeli

Hi All,

Is it possible to use includes dynamically in html.

Yes, but not with javascript, which runs on the client. SSI runs on the
server.
You're using SSI for the includes - use SSI for the whole thing. You can
use it to check a querystring param and conditionally include content.

http://www.carleton.ca/~dmcfet/html/ssi.html

--
--
~kaeli~
User: The word computer professionals use when they mean
'idiot'.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
R

Ron

Adam said:
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>
In addition to the two replies, you can also try using object elements
instead of server-side includes, which allows you to treat the content
of an object like an inline frame. Ie.,
<object id="file1" data="file1.html" type="text/html" width="100%"
standby="Loading file1.html...">
Your browser cannot render embedded HTML documents.
</object>
The difference is that your file1.html document should be a valid HTML
document, not just an HTML fragment. You can access that document
through the object's contentDocument property. I've used this method to
include sitewide menus on my websites.
 
K

kaeli

<object id="file1" data="file1.html" type="text/html" width="100%"
standby="Loading file1.html...">
Your browser cannot render embedded HTML documents.
</object>

I really like this method. This is the first time I've seen it for html
pages. Unfortunately, IE doesn't seem to support it.

Does anyone know what browsers do support this?
I tested Opera 7 and Netscape 7 and they were fine. IE 6, however,
didn't render anything at all, even the warning that it isn't supported.
Anyone test with Safari, Konqueror, etc? I assume Netscape 4 would not
support it.

I used the following doctype for both html files:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">

Should I use a different one for this?

--
 
R

Ron

kaeli said:
I really like this method. This is the first time I've seen it for html
pages. Unfortunately, IE doesn't seem to support it.

Does anyone know what browsers do support this?
I tested Opera 7 and Netscape 7 and they were fine. IE 6, however,
didn't render anything at all, even the warning that it isn't supported.
Anyone test with Safari, Konqueror, etc? I assume Netscape 4 would not
support it.

I used the following doctype for both html files:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">

Should I use a different one for this?
Heya kaeli,
I normally use the XHTML 1.1 standard with basic support for older
browsers, but it works in IE6 as well, at least if the document you're
calling is HTML. See the following two example pages in any browser:
http://slider142.com/xhtml/objecttest.xhtml - The base document is XHTML 1.1
http://slider142.com/xhtml/objecttest.html - The base document is HTML
4.01 Strict
 
K

kaeli

Heya kaeli,
I normally use the XHTML 1.1 standard with basic support for older
browsers, but it works in IE6 as well, at least if the document you're
calling is HTML. See the following two example pages in any browser:
http://slider142.com/xhtml/objecttest.xhtml - The base document is XHTML 1.1
http://slider142.com/xhtml/objecttest.html - The base document is HTML
4.01 Strict

You know, when I went to your link, it worked fine, but when I tried it
locally with my file, it didn't work (IE file->open).

I realized I left something out - the height attribute. Apparently IE
needs that more than Netscape and Opera do, as it was shrinking it to 0
px. :)
Your original example didn't have the height specified. Your code on
this link has it.

Thanks!

--
 
J

Jim Ley

Heya kaeli,
I normally use the XHTML 1.1 standard with basic support for older
browsers, but it works in IE6 as well,

no it does not, IE does not render XHTML 1.1. (other than as an XML
tree)
at least if the document you're
calling is HTML. See the following two example pages in any browser:
http://slider142.com/xhtml/objecttest.xhtml - The base document is XHTML 1.1
http://slider142.com/xhtml/objecttest.html - The base document is HTML
4.01 Strict

Use IFRAME.

Jim.
 
R

Ron

Jim said:
no it does not, IE does not render XHTML 1.1. (other than as an XML
tree)
Heya Jim,
The page I link to is standard XHTML 1.1 with document type and xml
prologue. It renders fine in IE 6 as HTML, not as an XML tree view, even
though IE doesn't recognize the document type.
Use IFRAME.

Jim.
I would use IFRAME as an example, but as it's been deprecated in XHTML
1.1 in favor of the coming XFrames standard, I decided to use object
instead. Even when writing HTML, I try to use elements that are
forwards-compatible for low maintenance.
 
K

kaeli

Heya Jim,
The page I link to is standard XHTML 1.1 with document type and xml
prologue. It renders fine in IE 6 as HTML, not as an XML tree view, even
though IE doesn't recognize the document type.

Actually, I got a download dialog box when I tried to open it.
http://slider142.com/xhtml/objecttest.xhtml

I have no clue what to do about that, but since I don't use xhtml, I
didn't worry about it.

--
 
J

Jim Ley

Heya Jim,
The page I link to is standard XHTML 1.1 with document type and xml
prologue. It renders fine in IE 6 as HTML, not as an XML tree view, even
though IE doesn't recognize the document type.

It doesn't render for me, I could make it render with some reg
hacking, but that's hardly a good thing, as it'll still be tag-soup,
and rendered badly.
I would use IFRAME as an example, but as it's been deprecated in XHTML
1.1 in favor of the coming XFrames standard, I decided to use object
instead. Even when writing HTML, I try to use elements that are
forwards-compatible for low maintenance.

Quick question then in a HTML doc embedded in OBJECT what does the A
element do - replace the object, or the parent - which is the spec
compliant way? (it's not defined I believe btw.)

Jim.
 
R

Ron

Jim said:
It doesn't render for me, I could make it render with some reg
hacking, but that's hardly a good thing, as it'll still be tag-soup,
and rendered badly.
Hmm, you're right. The XHTML file only renders in IE when I load it from
my computer. It may be because of the extra MSXML stuff I have installed.
Quick question then in a HTML doc embedded in OBJECT what does the A
element do - replace the object, or the parent - which is the spec
compliant way? (it's not defined I believe btw.)

Jim.
The object behaves like an IFRAME in Mozilla. Ie., manually add the
'target="_parent"' or "_top" attribute to links within, or use
javascript to cover all links automatically. Unfortunately, only
javascript seems to work for IE.
 
T

Thomas 'PointedEars' Lahn

kaeli said:
[...] (e-mail address removed) enlightened us with...
Is it possible to use includes dynamically in html.

Yes, but not with javascript, which runs on the client. [...]

s/javascript, which runs on the client/client-side J(ava)Script/

There is server-side J(ava)Script as well.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Ron said:
Heya Jim,

My name is not Jim.
The page I link to is standard XHTML 1.1 with document type and xml
prologue. It renders fine in IE 6 as HTML, not as an XML tree view, even
though IE doesn't recognize the document type.

XHTML served correctly as application/xhtml+xml does not render in IE at all
(I'm using the latest, Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;
Q312461; .NET CLR 1.1.4322)), it wants to download the document since it
does not support the MIME type. And serving XHTML as text/html is utter
nonsense. As for XHTML, see my signature.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top