Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
How can I call a javascript function without onclick event?? I want use Ajax code in my project without click , when I update the Mysql from another page, show automatic and immediately in my page.

HTML
<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","xxxxxx.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body  önload="showUser();">

 
<div id="txtHint"  runat="server"  önclick="showUser()">
<form id="txtHint" onchange="showUser(this.value) method="GET">


<table border="0" cellpadding="0" cellspacing="0" align="center" valign="center">
                style="width: 20%; height: 50%;">
                <tr>
                    <td bgcolor="Gray" class="style17" colspan="2" rowspan="2" valign="top">
               <?php        					  <br mode="hold" /???>$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";

$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

@mysql_select_db("HOUSEDATA") or die( "Unable to select database");

$query="SELECT Door FROM Door";

$result=mysql_query($query);
$image=mysql_result($result,0);
if($image == 1)
  	 echo  "<img src='Images/bulb_off.png'>";
if($image == 0)
      echo  "<img src='Images/bulb_on.png'>";
mysql_close();
?>
                    </td>
                   
            </tr></table>

</form>

</body>
</html>
Posted
Updated 20-Jan-17 15:22pm
v4

Have you tried the setInterval() javascript function?
It will automatically execute your code in a specified time(in milliseconds).
Usage:
JavaScript
setInterval( [you code or function call][, time interval in milliseconds] );

this will take care of updating your page via ajax without the need for user interaction.

Instead of calling your custom function in the onload event, you can try this:

HTML
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","xxxxxx.php?q="+str,true);
xmlhttp.send();
}

// create an interval calling the specified function every 10 seconds.
setInterval('showUser()',10000); 

//or you can use it like this
setInterval(function(){ showUser();}, 10000);
</script>
 
Share this answer
 
v4
Comments
Mohammad Ali_7 28-Nov-11 21:50pm    
I dont know how can I use in my code could u please explain more? where can I put this line? thank you for answer.
Mohammad Ali_7 28-Nov-11 22:11pm    
Thank youuuuuuuu , finally It's working. really thank you ;)
ast.lim12 28-Nov-11 22:17pm    
Thank you! Glad it worked for you.
It's very simple :

Call the JavScript Function/Code on body onLoad event.

JavaScript
<html>
<body  önload="showUser();">
    ...............
    ...............
    ...............
</body>
</html>


Or
JavaScript
<html>
<script language='javascript'>
function happycode(){
   alert('helo');
}
</script>
<body> 
<h1>Javascript call after page loaded</h1>
 
<script>
//call after page loaded
window.onload=happycode ; 
</script>
</body>
</html

Or Try this :

call a javascript function after a page load
 
Share this answer
 
v3
Comments
Mohammad Ali_7 27-Nov-11 10:51am    
Thank you for answering but doesn't work. Is not the solution ! I try before! any idea?
Manoj K Bhoir 27-Nov-11 11:33am    
Try updated solution.I hope it will help you.
Mohammad Ali_7 27-Nov-11 11:49am    
really tanx to helping, and maybe ur solution is right but still can not refresh the page when put it end of the page ! I tried to research
Mohammad Ali_7 27-Nov-11 11:52am    
maybe because my page is php , and this solution is for html, I want to refresh the page AUTOMATIC , without onclick or button, because I want refresh my PHP query in table !
i am need java-script function has calling in side to php code with out any button click otherwise condition is true to call the java-script function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900