By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

Problem with XHTML parsers with embedded Javascript

P: n/a

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_to_forward_to)
{
try
{
var xfamElem = document.getElementById('xFamily');
var mainform = document.getElementById('mainform');
var new_url = url_to_forward_to + "&variable=" + xfamElem.value;

mainform.action = new_url;
mainform.submit();
}
catch(error)
{
alert("InvokeURL encountered an error :" + error.description);
}
}

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

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


Rick wrote:
var new_url = url_to_forward_to + "&variable=" + xfamElem.value; 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 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_to_forward_to)
{
try
{
var xfamElem = document.getElementById('xFamily');
var mainform = document.getElementById('mainform');
var new_url = url_to_forward_to + "&variable=" + xfamElem.value;

mainform.action = new_url;
mainform.submit();
}
catch(error)
{
alert("InvokeURL encountered an error :" + error.description);
}
}
//]]>
</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_to_forward_to)
{
try
{
var xfamElem = document.getElementById('xFamily');
var mainform = document.getElementById('mainform');
var new_url = url_to_forward_to + "&variable=" + xfamElem.value;

mainform.action = new_url;
mainform.submit();
}
catch(error)
{
alert("InvokeURL encountered an error :" + error.description);
}
}
]]>
<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/
Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.