Question about Document Write

3 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.
    {{ DiscussionBoard.errors[11534520].message }}
    • Profile picture of the author dansbanners
      Originally Posted by aesoft View Post

      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.

      would then look like:

      Hope it helps.
      I just double checked, removing the second forward slash actually created another error message in the following:

      end tag for "img" omitted, but OMITTAG NO was specified ..."'+adBanner+'" width="125" height="125" alt="'+alt+'" title="'+title+'"></a>'); ✉
      You may have neglected to close an element, or perhaps you meant to "self-close" an element, that is, ending it with "/>" instead of ">".
      That's why I added the second forward slash in there in the first place. But thanks anyway.
      {{ DiscussionBoard.errors[11536681].message }}
  • Profile picture of the author e-mail2u
    Try removing the > so it looks like this

    Code:
    document.write('<a href="'+link+'" rel="nofollow" target="_blank"><img src="'+adBanner+'" width="125" height="125" alt="'+alt+'" title="'+title+'" </a>');
    {{ DiscussionBoard.errors[11582536].message }}
Avatar of Unregistered

Trending Topics