Problem with XHTML parsers with embedded Javascript

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

    #1

    Problem with XHTML parsers with embedded Javascript


    Hi.. I've got some code I wrote in PHP that will generate an HTML page
    with embedded javascript which in turn creates a new argument
    string for the browser, but the xhtml parser in Firefox and Opera both
    complain about my use of &var=value pairs in the generated URL.

    Below is my code that generates the string, and is part of an embedded
    HTML page (generated on the fly by a PHP script):

    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

  • Martin Honnen

    #2
    Re: Problem with XHTML parsers with embedded Javascript



    Rick wrote:
    [color=blue]
    > var new_url = url_to_forward_ to + "&variable= " + xfamElem.value;[/color]
    [color=blue]
    > 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..[/color]

    If you want to use an ampersand in XML you need to use the entity
    reference & or you need to wrap your text into a CDATA section.
    For script blocks in XHTML you can use e.g.

    <script type="text/javascript">
    //<![CDATA[
    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);
    }
    }
    //]]>
    </script>

    to write your code in a way that is both compatible with HTML and XML
    parsers.

    If your only aim is to write X(HT)ML served to XML parsers then of course
    <script type="text/javascript">
    <![CDATA[
    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);
    }
    }
    ]]>
    <script>

    Or simply put your script code into an external .js file and include
    that with

    <script type="text/javascript" src="file.js"></script>

    that way there will be no problems as the XML (or HTML) parser doesn't
    see the script code at all.




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Rick

      #3
      Re: Problem with XHTML parsers with embedded Javascript

      Martin,

      Thanks.. I'll try it out tonight! I guess I'll have to read up on these
      CDATA things as it's something I wasn't aware of. I did realize that I
      could have dumped this JS code into a separate file, but didn't really
      want to resort to doing that unless there were no other options.

      Thanks!

      -- Rick


      Comment

      Working...