Tutorial :Firefox error 'no element found'



Question:

First off, this isn't exactly the ideal way of setting up a page, however there's a need to distribute a script as 1 file.

I have a php script at the top of an otherwise xhtml document with javascript, and under certain conditions use XHR to send a query string to the page itself. The php at the top then activates, and stores the passed content as a session, and then kills itself (exit()). The XHR is async and is never checked to see if it returns content.

However in Firefox 3, the error console throws an error no element found every time the XHR request gets sent. Also, if I use an exit such as exit('Done'), Firefox throws a syntax error of (Done) as if it inserts it into the visible DOM. This doesn't seem to happen in Opera.

Is there a better way to store a session from an already generated xhtml page? Obviously I could XHR to another page, but I would prefer to keep it all on one script. Does Firefox treat XHR requests to self as updates to the DOM? I don't know why it's sending this error.


Update As I said, firefox only thows the error when the XHR request is made. The page is valid XHTML and works perfectly, without error unless the XHR request is made to the page itself.

I was wondering why it was sending the error because it really doesn't return anything.

Here's a javascript snippet that makes a ajax request from an object. It creates a XHR object, without a callback function, and posts the information. It works properly when not referencing the same page.

 var saveState = { saveContent: function(updateActiveMenu) {      var sendState = new ajaxObject(gV.url);      if (!updateActiveMenu) {          var storageContainer = document.getElementById("StorageContainer").innerHTML;          var menu = document.getElementById("Nav").innerHTML;          sendState.update("Containerstring="+urlencode(storageContainer)+"&Nav="+urlencode(menu)+"&Active="+gV.activeMenuItem, 'POST', true);    } }, }  

And the php does this

if (isset($_REQUEST['Containerstring']) && isset($_REQUEST['Nav']) && isset($_REQUEST['Active'])) {    $_SESSION['Containerarray'] = (saveContainer(regulateEscapes(urldecode($_REQUEST['Containerstring']))));    $_SESSION['Navarray'] = (saveNav(regulateEscapes(urldecode($_REQUEST['Nav']))));    $_SESSION['Active'] = $_REQUEST['Active'];    exit('Done');   }  

I'm also aware I shouldn't be using innerHTML but that's another story


The error is this

Error: no element found  Source File: http://localhost/ajax.php?1244648094055   Line: 1  

Note that the error, while on the php page I'm using, references a query string that is never called.


Solution:1

Firefox is expecting to get something it can parse as XML back, and throwing an XML parsing error when it gets an empty response.

Before your PHP calls "exit()", use

header('Content-Type: text/plain');  

and Firefox will not try to parse the response as XML, and there should be no error.


Solution:2

You should have your php return a valid HTTP response, because Firefox will try to read it even if you don't do anything with it. So return at least a valid header

header("HTTP/1.0 200");  exit();  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »