Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-01-2011, 11:29 AM   PM User | #1
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 795
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
AJAX Loading message

I have a search bar which list users depend what the person types which works well, however its a little slow and i'd like to show a loading message, how would i go about doing it?

Here's what i have so far

Code:
<script type="text/javascript">
function showResult(str)
{
	if (str.length==0) {
		document.getElementById("livesearch").innerHTML="";
		document.getElementById("livesearch").style.border="0px";
  		return;
  	}
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
  	}
	else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function() {
  		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
    		document.getElementById("livesearch").style.border="1px solid #A5ACB2";
    	}
  	}
	xmlhttp.open("GET","livesearch.php?u="+str,true);
	xmlhttp.send();
}
</script>
</head>

<body>
<form>
<input type="text" size="30" style="width:300px;" onkeyup="showResult(this.value)" />
<div id="livesearch" style="width:300px;" ></div>
</form>
I'd guess it would be a readyState thing but thats just a guess, im new to AJAX >.<
tomharto is online now   Reply With Quote
Old 06-01-2011, 12:15 PM   PM User | #2
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by tomharto View Post

Code:
<script type="text/javascript">
function showResult(str)
{
    if (str.length==0) {
        document.getElementById("livesearch").innerHTML="";
        document.getElementById("livesearch").style.border="0px";
          return;
      }
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
            document.getElementById("livesearch").style.border="1px solid #A5ACB2";
            document.getElementById('loadingDiv').style.display = 'none';
        }
      }
    xmlhttp.open("GET","livesearch.php?u="+str,true);
    xmlhttp.send();
    document.getElementById('loadingDiv').style.display = 'block';
}
</script>
</head>

<body>
<form>
<input type="text" size="30" style="width:300px;" onkeyup="showResult(this.value)" />
<div id="livesearch" style="width:300px;" ></div>
</form>
You could try something like the red bits.

First create a container with id = 'loadingDiv' that is hidden on page load. In it put your loading message.
bullant is offline   Reply With Quote
Old 06-01-2011, 01:12 PM   PM User | #3
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 795
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
I tried that but it didnt work . Does the display:block need to be earlier maybe?

Code:
<script type="text/javascript">
function showResult(str)
{
    if (str.length==0) {
        document.getElementById("livesearch").innerHTML="";
        document.getElementById("livesearch").style.border="0px";
          return;
      }
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
            document.getElementById("livesearch").style.border="1px solid #A5ACB2";
            document.getElementById('loadingDiv').style.display = 'none';
        }
      }
    xmlhttp.open("GET","livesearch.php?u="+str,true);
    xmlhttp.send();
    document.getElementById('loadingDiv').style.display = 'block';
}
</script>
</head>

<body>
<form>
<input type="text" size="30" style="width:300px;" onkeyup="showResult(this.value)" />
<div class="loadingDiv" style="display:none;">Loading&hellip;</div>
<div id="livesearch" style="width:300px;" ></div>
</form>
EDIT: DUH just noticed i had class rather than ID >.<. It works great thanks . JW if i wanted to use class rather than ID would i just put getElementByClass?
tomharto is online now   Reply With Quote
Old 06-02-2011, 12:47 AM   PM User | #4
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by tomharto View Post
]EDIT: DUH just noticed i had class rather than ID >.<. It works great thanks . JW if i wanted to use class rather than ID would i just put getElementByClass?
No problem

You could assign the div a class and then use a "getElementsByClass" function but it will return an array of elements with that class name. You would then need refer to a specific element by its index value in the array or loop through the array.
bullant is offline   Reply With Quote
Users who have thanked bullant for this post:
tomharto (06-02-2011)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:13 PM.


Advertisement
Log in to turn off these ads.