Question about Document Write

1 replies
For some reason, the following code:

document.write('<a href="'+link+'" target="_blank"><img src="'+adBanner+'" width="125" height="125" alt="'+alt+'" title="'+title+'" /></a>');

Is causing the following error:

document type does not allow element "a" here

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).
Not sure what to make of it? Can my document write code be written differently? Thanks.
#document #question #write
Avatar of Unregistered
  • Profile picture of the author aesoft
    Hello,
    In case you haven't been able to resolve this yet...
    You're closing the 'a' tag twice. A "self-closing" tag is when an element ends with a forward slash & greater than symbols (/>) and no ending tag. When an element ends with the tag, like ending with the '</a>', then it is not. The above code has both.

    Try removing the second forward slash before the end and see if that clears it up for you.
    document.write('<a href="'+link+'" target="_blank"><img src="'+adBanner+'" width="125" height="125" alt="'+alt+'" title="'+title+'" /></a>');
    would then look like:
    document.write('<a href="'+link+'" target="_blank"><img src="'+adBanner+'" width="125" height="125" alt="'+alt+'" title="'+title+'"></a>');
    Hope it helps.
    Signature

    - James

    {{ DiscussionBoard.errors[11534520].message }}

Trending Topics