/    Sign up×
Community /Pin to ProfileBookmark

compatability issue?

So I have a search function that searches through an XML file depending on which criteria the user wants to search. It works great so far in Firefox, OK in IE and not at all in Chrome. There’s only one problem I have with IE which is you have to click submit, you cant hit the enter key to search. Is this fixable? Ive tried writing a function where if it detects the enter key press, it “clicks” submit, but that didn’t work. In chrome, I search for something but it says there’s nothing to be found. My code is below and any help or a point in the right direction would be greatly appreciated.

[CODE]<script type=”text/javascript”>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}

function getType() {
for (var i=0; i < 3; i++) {
if (document.frmMain.criteria[i].checked) {
var rad_val = document.frmMain.criteria[i].value;
}
}
return rad_val;
}

window.onload = loadIndex;

function loadIndex() { // load indexfile
// most current browsers support document.implementation
if (document.implementation && document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument(“”, “”, null);
xmlDoc.load(“wdparts.xml”);
}
// MSIE uses ActiveX
else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async = “false”;
xmlDoc.load(“wdparts.xml”);
}
}

function searchIndex() { // search the index (duh!)
if (!xmlDoc) {
loadIndex();
}
// get the search term from a form field with id ‘searchme’

var searchterm = document.getElementById(“searchme”).value;
var searchtype = getType();
var allitems = xmlDoc.getElementsByTagName(“item”);
results = new Array;
if (searchterm.length < 3) {
alert(“Enter at least three characters”);
}
else {
// see if the XML entry matches the search term,
// and (if so) store it in an array
for (var i=0;i<allitems.length;i++) {
var name = allitems[i].getAttribute(searchtype);
var exp = new RegExp(searchterm,”i”);
if ( name.match(exp) != null) {
results.push(allitems[i]);
}
}
// send the results to another function that displays them to the user
showResults(results, searchterm);
}
}

// Write search results to a table
function showResults(results, searchterm) {

if (results.length > 0) {
// if there are any results, write them to a table

var reout = ‘You searched for <b><i>’+searchterm+'</i></b><br><br>’;
reout += ‘<table border=”1″ style=”width: 100%;”>’;
reout += ‘<tr><th>Manufacturer</th><th>Product Number</th><th>Description</th><th>Link</th></tr>’;
for(var i=0; i<results.length; i++) {
reout += ‘<tr>’;
reout += ‘<td>’ + results[i].getAttribute(“line”) + ‘</td>’;
reout += ‘<td>’ + results[i].getAttribute(“pnum”) + ‘</td>’;
reout += ‘<td>’ + results[i].getAttribute(“description”) + ‘</td>’;
reout += ‘<td>’ + results[i].getAttribute(“link”) + ‘</td>’;
reout += ‘</tr>’;
}
reout += ‘<table>’;
document.getElementById(‘test’).innerHTML = reout;
} else {
// else tell the user no matches were found
alert(‘No results found for ‘+searchterm+’!’);
}
}
</script>[/CODE]

[CODE]<p><form name=”frmMain” id=”frmMain” action=””>
<b>Search by:&nbsp;&nbsp;</b>
<input type=”radio” name=”criteria” value=”line” checked=”checked”>Manufacturer &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;
<input type=”radio” name=”criteria” value=”pnum”>Product Number &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<input type=”radio” name=”criteria” value=”description”>Description &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<br><br>
<input id=”searchme” type=”text” size=”20″>&nbsp;&nbsp;
<input value=”Search” id=”btnSearch” onclick=”searchIndex(); return false;” type=”submit”>
</form></p>
<p id = “test”></p>
</div>[/CODE]

EDIT: I also noticed that when searching something, it returns any word containing the search string. For example, if I search for “air”, it’ll return items with “repair”in the description. How do I edit the code so that it searches for an exact match?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@toicontienOct 18.2011 — In order for hitting the ENTER key to submit a form, Internet Explorer needs these two things:

-

1) A form with a valid URL in the action attribute (even a simple "#" will suffice)

2) At least one submit button (<input type="submit|image">, <button type="submit">)

  • - The combination of these two requirements allows Internet Explorer to submit a form when pressing ENTER on text fields.
  • Copy linkTweet thisAlerts:
    @KorOct 19.2011 — In order for hitting the ENTER key to submit a form, Internet Explorer needs these two things:

    -

    1) A form with a valid URL in the action attribute (even [COLOR="Red"]a simple "#"[/COLOR] will suffice)

    .[/QUOTE]

    Not necessarily. An empty string is enough as being the action's URL (action=[COLOR="Blue"]""[/COLOR]). It is a valid address, and it will self submit the data.
    ×

    Success!

    Help @MisterYoji spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...