xmlHttp.responseText is empty if call triggered by carriage return

Posted on 2009-04-16
Last Modified: 2012-05-06
I have a web menu where the user clicks  a search link which opens a small window where a ticket number can be keyed in.  The javascript to create the small window is below.  If the user enters the ticket number, then clicks on the button called 'search', it all works as one would expect.  If the user puts a ticket number in and presses the return key, the responseText comes back as empty.  The perl routine just returns the ticket number if it is valid.

The alert(url) shows the correct call to, whether it's triggered by the enter key or the click on the 'Search' button.  However, the return data is the ticket number only when the button is pressed, and not when the enter key is struck.  I want them to both give the same result.
function searchBox(){

  var myWin = open("","SearchWin","width=400,height=200,menubar=no,scrollbars=no,titlebar=no,toolbar=no,alwaysRaised=yes");

myWin.document.writeln('<script type="text/javascript">');

myWin.document.writeln('function doSearch(){');

myWin.document.writeln('var xmlHttp;');

myWin.document.writeln('try  {  xmlHttp=new XMLHttpRequest();  } catch (e) {');

myWin.document.writeln('  try    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    }  catch (e) {');

myWin.document.writeln('    try      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      }    catch (e){');

myWin.document.writeln('      alert("Your browser does not support AJAX!");');

myWin.document.writeln('      return false;');

myWin.document.writeln('      }');

myWin.document.writeln('    }');

myWin.document.writeln('  }');

myWin.document.writeln('xmlHttp.onreadystatechange=function() {');


myWin.document.writeln('  {');

myWin.document.writeln('  var ticketExists=xmlHttp.responseText;');

myWin.document.writeln('  alert("response " + ticketExists);');

myWin.document.writeln('  if (ticketExists != ""){');

myWin.document.writeln('	window.opener.location.href=""+ticketExists;');

myWin.document.writeln('  } else {');

myWin.document.writeln('	alert("Ticket does not exist");');

myWin.document.writeln('  }');

myWin.document.writeln('  window.close();');




myWin.document.writeln( 'var ticket=document.getElementById("ticket").value;');

myWin.document.writeln( 'ticket = ticket.replace(String.fromCharCode(13),"");');

myWin.document.writeln( 'ticket = ticket.replace(String.fromCharCode(10),"");');

myWin.document.writeln('var url=""+ticket;alert(url);');






  myWin.document.writeln('<form name="searchForm" id="searchForm">');

  myWin.document.writeln('<table><tr><td>Ticket</td><td><input type="text" name="ticket" id="ticket" onKeyPress="if (event.keyCode==13){doSearch();}"></td></tr>');

  myWin.document.writeln('<tr><td></td><td><input type="button" name="search" id="search" value="Search" onClick="doSearch();">');

  myWin.document.writeln('<input type="button" name="cancel" id="cancel"value="Cancel" onClick="window.close();"></td></tr></table></form>');





Open in new window

Question by:jpopowski
    LVL 18

    Accepted Solution

    I think the problem is that when the user hits enter the form is submitting and not calling your JavaScript function.

    Try adding the following to the form tag

    onsubmit="doSearch(); return false"

    Author Closing Comment

    Thank you so much!  I been struggling with this for a couple of evenings and just didn't know what to try next.  I really appreciate it.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 Mobile App Development with PhoneGap

    PhoneGap can help you leverage your already existing HTML5, JavaScript, and CSS skills in order to create and deploy cross-platform mobile apps.This program comprehensively covers HTML5 mobile app development from top to bottom.

    Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    This video discusses moving either the default database or any database to a new volume.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now