如何自动调用Javascript代码 - 没有OnClick! [英] How to call Javascript code automatically - no OnClick !
问题描述
如何在没有onclick事件的情况下调用javascript函数?我希望在没有点击的情况下在我的项目中使用Ajax代码,当我从另一个页面更新Mysql时,立即自动显示在我的页面中。
< html >
< head >
< script < span class =code-attribute> type = text / javascript >
function showUser(str)
{
if (str == )
{
document .getElementById( txtHint)。innerHTML = 跨度>;
return ;
}
if ( window .XMLHttpRequest)
{ // IE7 +,Firefox,Chrome,Opera,Safari的代码
xmlhttp = new XMLHttpRequest();
}
其他
{ // IE6代码,IE5
xmlhttp = 新 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();
}
< / 脚本 >
< / head >
< body önload = showUser(); >
< div id = txtHint runat <跨班=code-keyword> = 服务器 önclick = showUser( ) >
< 表格 id = txtHint onchange = showUser(this.value)method = GET >
< 表 border = 0 cellpadding = 0 cellspacing = 0 align = center valign = center >
style =width:20%;高度:50%;>
< tr >
< td bgcolor = 灰色 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)
或
trigger_error(mysql_error(),E_USER_ERROR);
@mysql_select_db(HOUSEDATA)或死亡(无法选择数据库);
$ query =SELECT Door From Door;
$ result = mysql_query($ query);
$ image = mysql_result($ result,0);
if($ image == 1)
echo< img < span class =code-attribute> src =' images / bulb_off.png' > ;
if($ image == 0)
echo< img < span class =code-attribute> src =' images / bulb_on.png' > ;
mysql_close();
?>
< / td >
< / tr > < / table >
< / form >
< / body >
< / html >
hostname_localhost =localhost;
database_localhost =mydatabase;
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. 这篇关于如何自动调用Javascript代码 - 没有OnClick!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<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>
hostname_localhost ="localhost";
database_localhost ="mydatabase";
username_localhost ="root";