Javascript代码我无法理解

编程入门 行业动态 更新时间:2024-10-23 07:22:15
本文介绍了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, 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: www.w3schools /ajax/default.asp [ ^ ] 最后一行代码重新加载浏览器,这也将清除txtHint中的值。如果这不是您想要的,那么请将其注释如下:

// window.location.reload();

Peter Leow所列网站上的链接可能会对您有所帮助。 www.w3schools/php/php_ajax_php.asp [ ^ ]

请查看我对该问题的评论。 以防万一,两个提示: 调用 window.XMLHttpRequest 表示AJAX:en.wikipedia/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: www.w3schools/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. www.w3schools/php/php_ajax_php.asp[^]

Please see my comment to the question. Just in case, two hints: The call window.XMLHttpRequest means AJAX: en.wikipedia/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代码我无法理解

本文发布于:2023-11-08 16:03:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!