473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.create Element is not XHTML standard :(

Hi,

I've been creating a script that dynamic loads js files.

but after creating that script, (and i use
document.create Element('script ');) in that function.. i've realise that
the code that shows up in the browser is:

<script type="text/javascript">

should it be

<script type="text/javascript" />

or

<script type="text/javascript"></script>
???? thanks

Jan 2 '07 #1
4 3017
Narven wrote:
I've been creating a script that dynamic loads js files.
Good luck getting them to evaluate in the correct frame context!
but after creating that script, (and i use
document.create Element('script ');) in that function.. i've realise that
the code that shows up in the browser is:

<script type="text/javascript">

should it be

<script type="text/javascript" />

or

<script type="text/javascript"></script>
The pipeline goes

HTTP ->
XHTML or HTML ->
DOM

DOM is a tree of objects in memory. They no longer have a script
representation. When you call createElement() , it creates one of these
objects directly, so the question whether the object "came from" XHTML or
HTML is irrelevant.

Next, by "the code that shows up in the browser", you might mean the return
value of innerHTML. It would be nice if this were indeed XHTML, but nobody
will ever parse and consume this HTML (unless you tell them to). The DOM
might be recreating its HTML from its data objects, and you might be
witnessing this recreation.

Next, if you switch your HTTP document type from HTML to XHTML, with code
like this...

def set_encoding
if request.env["HTTP_ACCEP T"] &&
request.env["HTTP_ACCEP T"].index('applica tion/xhtml+xml')
headers["Content-Type"] = "applicatio n/xhtml+xml; charset=utf-8"
else
headers["Content-Type"] = "text/html; charset=utf-8"
end
end

(borrowed from Rails), then your innerHTML might change its story. Or maybe
not.

Next, if you switch your document type, you might break support for browsers
that have not caught up with this millenium. That's important to some
people, so I bring it up here to avoid the inevitable tilting at
windmills...

Try inserting a single space into the createElement() , with createTextNode( )
or some other browser-specific equivalent. Then the system might have no
choice but to close the tag correctly with a </script>.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Jan 2 '07 #2
Narven wrote:
I've been creating a script that dynamic loads js files.
You could try:

document.href = "javascript:voi d(function(){"+ x+"})"

Then you stuff the contents of the JS file into x and escape it all as a
string. Other variations apply. Sometimes this javascript:void trick is
overlooked, and sometimes it's more convenient than other options.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Jan 2 '07 #3
Phlip said the following on 1/2/2007 1:54 AM:
Narven wrote:
>I've been creating a script that dynamic loads js files.

Good luck getting them to evaluate in the correct frame context!
Why do you need luck to accomplish something that is trivially simple?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 2 '07 #4
Phlip wrote:
<snip>
The pipeline goes

HTTP ->
XHTML or HTML ->
DOM
That last line should be XHTML DOM or HTML DOM respectively, as the two
types of DOM are distinct.
DOM is a tree of objects in memory.
It is.
They no longer have a script representation.
What is that supposed to mean?
When you call createElement() , it creates one of these
objects directly, so the question whether the object
"came from" XHTML or HTML is irrelevant.
If you are creating an element in an XHTML document then it should be
the createElementNS method that is used as XHTML is namespace
qualified. Of course in reality most who think they are using XHTML are
actually using tag soup HTML with lots of errors in it, and so
scripting an HTML DOM in reality.
Next, by "the code that shows up in the browser", you might mean the
return value of innerHTML. It would be nice if this were indeed XHTML,
but nobody will ever parse and consume this HTML (unless you tell them
to). The DOM might be recreating its HTML from its data objects, and
you might be witnessing this recreation.
The - innerHTML - property is largely unsupported in XHTML DOMs.
Next, if you switch your HTTP document type from HTML to XHTML, with code
like this...

def set_encoding
if request.env["HTTP_ACCEP T"] &&
request.env["HTTP_ACCEP T"].index('applica tion/xhtml+xml')
headers["Content-Type"] = "applicatio n/xhtml+xml; charset=utf-8"
This is going to try to send application/xhtml+xml content type headers
to any UA that asserts its rejection of application/xhtml+xml. That is
irrational, and based upon a superficial understanding of HTTP accept
headers.

But given that XHTML DOMs and HTML DOMs need to be scripted
differently for anything beyond the utterly trivial, having served
alternative content-type headers how does this handle the problem of
needing to serve alternative scripts with the two content types?
else
headers["Content-Type"] = "text/html; charset=utf-8"
end
end

(borrowed from Rails),
Oh dear. I knew that the 'rails community' was deficient in their
understanding of javascript but I was not expecting such ignorance of
HTTP to be so evident.
then your innerHTML might change its story. Or maybe
not.
Yes, whenever the browser is sent the application/xhtml+xml content
type header with a valid XHTML document, and it supports XHTML, and is
scriptable, it will create an XHTML DOM and the odds are that -
innerHTML - will not be supported.
Next, if you switch your document type, you might break support for
browsers that have not caught up with this millenium.
If you use that server code for the switch you may end up breaking the
site for fully HTTP 1.1 conforming browser regardless of their age.
That's important to some people, so I bring it up here to avoid the
inevitable tilting at windmills...
There is a great deal to be said for people understanding the
implications of what they are doing.
Try inserting a single space into the createElement() , with
createTextNode( ) or some other browser-specific equivalent. Then the
system might have no choice but to close the tag correctly with
a </script>.
You point out that createElement creates the DOM representation of the
HTML element post mark-up, and then suggest that adding a text node
might have "the system" "close the tag correctly", when tags are parts
of the mark-up, and so not parts of the DOM.

Richard.

Jan 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
3436
by: jullag | last post by:
Hi, does anyone know of any javascript method that does the same job as document.write(), but not necessarily at the end of the document? For instance, insert some text inside an element that has a specific ID tag? thanks a lot JL
1
3412
by: alex.sherwin | last post by:
Working on a google homepage module. One of the things I do is retrieve results from a search into a string. I want to be able to access all the anchors in this string, (their name, href, and innerHTML) and using regex for string parsing would be tiresome and difficult for this. I've searched far and wide, but I can't seem to find out if...
1
2708
by: JackieWilson | last post by:
Hello! I', trying to create DOM based XML document. First I create XmlDocument object and then read other XML DOM Document and modifie the first one. I'm putting Noden and Attributes tto the first XML DOM document when needed. Everything seems to be allrigth but when I trying to save the scratch based DOM Document wirj following code I get...
10
5571
by: James Black | last post by:
It appears that this is actually a difference between whether to use DOM2 or DOM1. I am trying to write my programs using XHTML for the webpage, but, I have to use DOM2 for xhtml 1.1. Now, how much of a problem will this be for browser compatibility? I am using a php script to change the mime type to be correct, either...
6
1786
by: raoul | last post by:
MSIE 6.0 apparently does not support protyping with objects created with document.createElement, while Firefox does. I tested it by typing it into the adress bar, but it also appears to be the case for code embedded in a HTML document. Here's a simple segment of code to demonstrate the difference: javascript: function...
10
4247
by: webEater | last post by:
Hello, I try the following in Firefox and other modern browsers: window.addEventListener('load', function() { document.title = CSS.getClass('fontSize'); var div = document.createElement('div'); document.getElementsByTagName('body').appendChild(div); alert(div); alert(div.style) }, true);
3
6348
by: SMH | last post by:
Normally an SVG document is loaded/parsed/interpreted inside an HTML document using an 'object' (or 'embed') element, although there are supposedly other ways too. The problem is, the SVG document must be static this way. I want to use the DOM interface to build SVG dynamically inside an HTML document. I am guessing I can build it inside...
23
5891
by: Stanimir Stamenkov | last post by:
<form name="myForm" action="..."> <p><input type="text" name="myElem"></p> </form> As far as I was able to get the following is the standard way of accessing HTML form elements: document.forms.elements But I have also seen the following:
23
6556
by: vunet | last post by:
It is recommended by some sources I found to create IFrames in IE using document.createElement('<iframe src="#">') instead of document.createElement('iframe'). Why and what browser versions to use it? IE5 or IE6? Thanks
0
7432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7943
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...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
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...
0
5076
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.