Problem with XHTML parsers with embedded HTML (e.g. firefox et-al)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rick

    Problem with XHTML parsers with embedded HTML (e.g. firefox et-al)

    Hi.. I've got some code I wrote in PHP that will generate a new argument
    string for the browser, but the xhtml parser in Firefox and Opera both
    complain about my use of &var=value pairs.

    Below is my code that generates the string :

    function InvokeURL(url_t o_forward_to)
    {
    try
    {
    var xfamElem = document.getEle mentById('xFami ly');
    var mainform = document.getEle mentById('mainf orm');
    var new_url = url_to_forward_ to + "&variable= " + xfamElem.value;

    mainform.action = new_url;
    mainform.submit ();
    }
    catch(error)
    {
    alert("InvokeUR L encountered an error :" + error.descripti on);
    }
    }

    This works fine with *many* browsers except the new ones.. With IE on
    windows (IE6) and Opera, Firefox all complain, Safari and some older
    browsers seem to work OK..

    If I change the line with the new_url variable setting on it to use a
    & instead, it passes the XHTML tests but no longer works at all.. When
    using the & it still has that in the URL that shows in the address
    bar and hence it seems to cause the script on the other end a lot of
    problems as it is unable to properly parse the URL string.

    Any ideas on how to portably get around this problem with embedded &
    created URL's?

    Many thanks in advance!

    -- Rick
  • 2metre

    #2
    Re: Problem with XHTML parsers with embedded HTML (e.g. firefox et-al)

    Rick wrote:[color=blue]
    > Hi.. I've got some code I wrote in PHP that will generate a new argument
    > string for the browser, but the xhtml parser in Firefox and Opera both
    > complain about my use of &var=value pairs.
    >
    > Below is my code that generates the string :
    >
    > function InvokeURL(url_t o_forward_to)
    > {
    > try
    > {
    > var xfamElem = document.getEle mentById('xFami ly');
    > var mainform = document.getEle mentById('mainf orm');
    > var new_url = url_to_forward_ to + "&variable= " + xfamElem.value;
    >
    > mainform.action = new_url;
    > mainform.submit ();
    > }
    > catch(error)
    > {
    > alert("InvokeUR L encountered an error :" + error.descripti on);
    > }
    > }[/color]

    That's javascript. I'd ask in a javascript newsgroup.

    Comment

    • John Dunlop

      #3
      [OT] XHTML script element content [was: Problem with XHTML ... ]

      It would be more appropriate to consult an (X)HTML group.
      These issues have been discussed at length in the past, so
      the clues are in the archive. I don't see how any of what
      follows relates to PHP though.

      Rick wrote:
      [color=blue]
      > the xhtml parser in Firefox and Opera both complain about my
      > use of &var=value pairs.[/color]

      Rightly so. The content of SCRIPT in HTML is CDATA; in
      XHTML, #PCDATA. So in XHTML script elements, entity
      references are recognised. Ampersands that should be
      treated literally must be escaped. XHTML1.0 sec. 4.8 says:

      | In XHTML, the script and style elements are declared as
      | having #PCDATA content. As a result, < and & will be
      | treated as the start of markup, and entities such as &lt;
      | and &amp; will be recognized as entity references by the
      | XML processor to < and & respectively. Wrapping the
      | content of the script or style element within a CDATA
      | marked section avoids the expansion of these entities.



      But in HTML, even though SCRIPTs have CDATA content, entity
      references are not recognised; '&' is never the start of an
      entity reference. HTML4.01 sec. 6.2 says:

      | Although the STYLE and SCRIPT elements use CDATA for their
      | data model, for these elements, CDATA must be handled
      | differently by user agents. Markup and entities must be
      | treated as raw text and passed to the application as is.


      [color=blue]
      > &variable[/color]

      In XHTML the ampersand must be escaped.
      [color=blue]
      > This works fine with *many* browsers except the new ones.[/color]

      Tag soup slurpers might give it the impression of working,
      but it's wrong.
      [color=blue]
      > If I change the line with the new_url variable setting on it to use a
      > &amp; instead,[/color]

      Then it becomes valid XHTML.
      [color=blue]
      > it passes the XHTML tests but no longer works at all.[/color]

      Because most browsers don't grok XHTML.
      [color=blue]
      > Any ideas on how to portably get around this problem with embedded &
      > created URL's?[/color]

      In increasing order of preference:

      1. Wrap the script's content in a CDATA section (XML1.0
      sec. 2.7) and don't encode the ampersands.

      2. Stop using inline scripts; move them to a separate
      file.

      3. Stop sending XHTML to browsers which don't support it.
      You might choose to use content negotiation to send XHTML
      to browsers which do support it and HTML to ones which
      don't. Why bother?

      --
      Jock

      Comment

      Working...