JavaScript redirect fails in Zoop 1.2 on Opera 9 or Firefox 2 for Ubuntu 7.10

When doing AJAX form processing I needed to do a JavaScript redirect when a valid value was received to move to the next step, but it was not working. The function below is a simple post function to demonstrate what I was seeing.

public function postTest($inPath) {
    //Check for successful post would go here
    if($success) {
       self::zoneRedirect('NextPage',JS_REDIRECT);
    }
}

The problem is the HTML comment in the utils.php on line 63 in the Redirect() function. I believe it needs to be surrounded in a CDATA comment tag, but I got it to work, by just removing the HTML comment all together. It would appear that these browsers treat all HTML comments as a comment regardless of the parent tag.

function Redirect( $URL, $redirectType = HEADER_REDIRECT)
{
	switch($redirectType)
	{
		case HEADER_REDIRECT:
			header("location: $URL");
			break;
		case JS_REDIRECT:
			//Original
			// echo("<script language=\"JavaScript\" type=\"text/javascript\"><!-- top.location.href = \"$URL\"; --></script>");
			//Fixed
			echo("<script language=\"JavaScript\" type=\"text/javascript\">top.location.href = \"$URL\";</script>");
			break;
		default:
			trigger_error("unknown redirect type");
			break;
	}
	exit();
}

CDATA

can you test it with the CDATA in place:

echo("<script language=\"JavaScript\" type=\"text/javascript\"><![CDATA[ top.location.href = \"$URL\"; ]]></script>");

And let us know if that works?

CDATA also fails

Adding the CDATA results in the same failure to execute the code. The more I look into this I suspect it may be the prototype library causing the bug and not the browsers because I am evaluating the Javascript with an AJAX Update call. The data receive by the AJAX Update is just Javascript without any HTML or Meta Data. Currently I am using prototype 1.6.0.2. I could also post this in the prototype forums, but the Zoop community should know since prototype was packaged with Zoop.

As a test I added the code below to output the Meta Data first to see if the browser was interpreting the javascript incorrectly, but this also did not correct the CDATA issue or HTML comment issue. This again points me to the prototype library, thoughts?

function Redirect( $URL, $redirectType = HEADER_REDIRECT)
{
	switch($redirectType)
	{
		case HEADER_REDIRECT:
			header("location: $URL");
			break;
		case JS_REDIRECT:
			echo('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
			echo("<script language=\"javascript\" type=\"text/javascript\"><![CDATA[ top.location.href = \"$URL\"; ]]></script>");
			break;
		default:
			trigger_error("unknown redirect type");
			break;
	}
	exit();
}

Prototype and evaluating Javascript

One of the options described here:
http://www.prototypejs.org/api/ajax/options
explains how to make sure that prototype evaluates your script.
In order to make this work, we'd need to send a different content-type when doing an AJAX redirect. I think the safest thing to do here, is create a new redirect type, AJAX_REDIRECT.
so:

define("AJAX_REDIRECT", 3);
 
function Redirect( $URL, $redirectType = HEADER_REDIRECT)
{
	global $globalTime;
	switch($redirectType)
	{
		case HEADER_REDIRECT:
			header("location: $URL");
			break;
		case JS_REDIRECT:
			echo("<script language=\"JavaScript\" type=\"text/javascript\"><![CDATA[ top.location.href = \"$URL\"; ]]></script>");
			break;
		case AJAX_REDIRECT:
			header("content-type: text/javascript");
			echo("top.location.href = \"$URL\";");
			break;
 
		default:
			trigger_error("unknown redirect type");
			break;
	}
	logprofile($globalTime);
	exit();
}

Can you test this, and see if it works for you?

AJAX and javascript redirect.

In fact, I believe that any javascript(in <script/> tags) received from an AJAX request is not evaluated by the browser, unless you as the handler do something special to handle it. I think that I have run into this issue in the past, and I had a way to handle it. I'll look and see if I can find it.

The Solution Works (Kind Of)

The solution you sent does work, but before evaluating the script the div is populated with top.location.href = \"$URL\"; because I am using Ajax.Updater to call the URL. Without the script tag the javascript is visible. It looks as if Ajax.Updater will always populate the id passed in before evaluating any script. If I was not using Ajax.Updater I may never have seen this in the page.

I have never had any issues using the script tag when processing text/html content via AJAX. Is it not supposed to be valid? The dhtml below will work without issue using Ajax.Updater with evalScripts: true when updating a div on another page with the supplied content.

<div>Here is the text. You should also see a message.</div>
<script>alert('Hello there you called with AJAX?');</script>

2008

Well, then I see no reason to not do it with script tags, without cdata or comment tags, and just leave it as js_redirect. I'll commit that change.