Javascript代码我无法理解 [英] Javascript Code I cannot understand

查看:96
本文介绍了Javascript代码我无法理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在查找程序中的错误时遇到问题并且我有这段代码,但我不明白。有人可以向我解释一下。提前致谢。



  function  addItem(str){
if (str == ){
document .getElementById( txtHint)。innerHTML = ;
return ;
}
if window .XMLHttpRequest){
// IE7 +,Firefox,Chrome,Opera,Safari的代码
xmlhttp = new XMLHttpRequest();
} 其他 { // 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 checkinout-actions / add-more-items-code.php?q = + str,< span class =code-keyword> true
);
xmlhttp.send();
window location .reload();
}

解决方案

这是一个带有名为str的参数的javascript函数。

此代码将清除名为txtHint的html元素的值,如果参数为空则返回:

  if (str ==  ){
document.getElementById( txtHint)。innerHTML = ;
return ;
}



否则,代码的第二部分将此参数作为查询字符串传递给名为add-more-items-code的服务器端脚本.php使用Ajax方法。此服务器端脚本的响应将显示在txtHint中。您可以通过本教程了解Ajax:

http://www.w3schools.com /ajax/default.asp [ ^ ]

最后一行代码重新加载浏览器,这也将清除txtHint中的值。如果这不是您想要的,那么请将其注释如下:

 // window.location.reload(); 


Peter Leow所列网站上的链接可能会对您有所帮助。



http://www.w3schools.com/php/php_ajax_php.asp [ ^ ]


请查看我对该问题的评论。



以防万一,两个提示:



调用 window.XMLHttpRequest 表示AJAX:http://en.wikipedia.org/wiki/Ajax_(programming) [ ^ ]。



和电话 xmlhttp = new ActiveXObject(Microsoft.XMLHTTP)表示使用JavaScr 可能造成的最大滥用之一 ipt,以及假设有人会使用IE5或IE6。甚至微软也建议完全否认向这些用户提供任何Web内容,这可能纯粹是假设的。使用 ActiveXObject 也是完全不安全的,真正的浏览器也不支持它。



-SA

Hi
I am having trouble looking for the error in my program and I have this code but I do not understand it. Can someone explain it to me. Thanks in advance.

function addItem(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","checkinout-actions/add-more-items-code.php?q="+str,true);
	  xmlhttp.send();
	  window.location.reload();
	}

解决方案

The is a javascript function that takes an argument called "str".
This code will clear the value of an html element called "txtHint" and return if the argument is empty:

if (str=="") {
  document.getElementById("txtHint").innerHTML="";
  return;
}


Otherwise, the second part of the code is passing this argument as query string to a server-side script called "add-more-items-code.php" using Ajax method. The response from this server-side script will be displayed in "txtHint". You can learn about Ajax by going through this tutorial:
http://www.w3schools.com/ajax/default.asp[^]
The last line of code reload your browser, this will also clear the value in the "txtHint". If that is not what you want, then comment it out like this:

//window.location.reload();


This link at the site Peter Leow listed might help you.

http://www.w3schools.com/php/php_ajax_php.asp[^]


Please see my comment to the question.

Just in case, two hints:

The call window.XMLHttpRequest means AJAX: http://en.wikipedia.org/wiki/Ajax_(programming)[^].

And the call xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") means one of the biggest abuse one can possibly do using JavaScript, as well as assuming that somebody would use IE5 or IE6. Even Microsoft advised to completely deny serving any Web content to such users, probably purely hypothetical. Using ActiveXObject is utterly unsafe, too, and "real" browser won't support it.

—SA


这篇关于Javascript代码我无法理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆