标签:error

浏览模式: 标准 | 列表 | 图文
如何调试JS错误

如何调试JS错误

做网页开发的时候不可避免会用到JS,当然更不可避免的就会遇到JS错误。当你打开网页时,IE左下角的感叹号是不是让你很郁闷?怎么调试就成了必修课了。

一般来说,IE的出错信息还是能够基本定位到代码中,FF的相对定位的就更准确一些。只是有时候IE报错为0行或者N行(这个N超出了页面行数)时,就比较让人郁闷。而更头疼的是,如果这个代码在FF完全正常,那真是想死的心都有了。

IE下面,可能更多的时候还是利用微软自己提供的工具来进行查错:ms script debugger,只是知道的人很少,用的人更少。现在firebug lite已经登录IE平台(其实也不算是登录,只是加载firebug lite类库而已。不过,这已经能解决大部分问题了)。FF下面嘛,那就主推firebug,随着firebug的流行,一些附加的东西都有了,如firephp,firecookie,eventbug等插件也越来越让人离不开它。safari和chrome这些基于webkit的浏览器。也都有着自带的js调试工具,可用性我就不太清楚了。没用过,更多的时候只保证IE和FF正常就OK了。webkit虽然份额随着国内所谓双核浏览器的开发而增长的较快,但真正的使用者还仅限于那一小撮人群而已。

于是,这,又成了firebug等的天下(IE8已经自带调试工具,快捷键与firebug一样,界面差不多)。

不废话,下面是我转贴的文章,介绍了一些常见的调试方法,其实也不能算是调试,也可以说是代码的一些写法罢了。【略做整理】

A 使用alert() 和document.write() 方法监视变量值
如果要中断代码的运行,监视变量的值,则使用alert() 方法;
如果需要查看的值很多,则使用document.write() 方法,避免反复单击“确定”按钮;

B 使用window.onerror 事件
当页面出现异常时,onerror 事件会在window 对象上触发。它能在一定程度上告诉开发者相关的错误信息。
示例:

<script type="text/javascript">
function myerror(_message,_url,_line)
{
alert("错误信息:" + _message
+"\n错误的URI:" + _url
+"\n错误的行数:" + _line
);

return true; //屏蔽系统的事件
}
//绑定错误事件
window.onerror = myerror;

//触发错误示例:
window,onload = test;
</script>
注意:在IE 中,触发error 事件后,正常的代码会继续运行,所有的变量和数据都会保存下来,在其onerror 事件处理方法中可以正常访问到;而在Firefox 中,触发error 事件后,一切都结束,所有的变量和数据都将被销毁。

C 使用 try...catch 语句找错误
示例:
<script type="text/javascript">
try
{
alert(触发异常);
}
catch (_ex) //可以省略“_ex”参数
{
var err = "错误信息";
for (var i in _ex)
{
err += "\n参数名:" + i
+ "\t参数值:" + _ex[i];
}
alert(err); //打印错误
}
finally //finally 可以被省略...
{
alert("finally 总是会运行");
}
</script>

注意:try...catch 并不能很好的处理JavaScript 的语法错误。
示例:
<script type="text/javascript">
try
{
alert("触发语法错误")); //多了半边“)”
}
catch (_ex) //可以省略_ex参数
{
var err = "错误信息";
for (var i in _ex)
{
err += "\n参数名:" + i
+ "\t参数值:" + _ex[i];
}
alert(err); //打印错误
}
</script>
该示例并没有进入catch 块中。

D 使用相关调试器
在IE 和Firefox 浏览器中,可以使用相关的调试器或插件对JavaScript 进行调试。

● 在Firefox 浏览器中,可以使用其自带的“错误控制台”。操作步骤如下:
打开Firefox 浏览器 → 在菜单条“工具”中 → 选择“错误控制台”即可。
在没有其他插件的情况下,其自带的“错误控制台”是一个非常不错的选择。

另外,在Firefox 浏览器中,还有一些很不错的调试器,如:Venkman、Firebug 等。
Venkman 调试器安装后,可以在Firefox 浏览器 → 在菜单条“工具”中 → 选择“JavaScript Debugger ”命令启用;
Firebug 调试器安装后,可以在Firefox 浏览器 → 在菜单条“工具”中 → 选择“Firebug”→ 选择“打开 Firebug”即可;

● 在IE 浏览器中,可以使用 Microsoft Script Debugger 调试器
Microsoft Script Debugger 是微软随IE 4 一同发布的一个IE插件,可以从微软的官方网站上免费下载。
下载安装以后,必须将IE 浏览器的调试选项打开才能使用。操作步骤如下:
1> 打开IE 浏览器 → 选择菜单栏的“工具”→ “Internet 选项”命令 → “高级”选项卡 → 将“禁用脚本调试(Internet Explorer )”复选框中的勾去掉即可。
2> 当IE 浏览器正在浏览页面时,运行Microsoft Script Debugger 调试器工具即可进行调试。
在Microsoft Script Debugger 调试器的 Running Document 面板中选择开启的页面文件(只读),然后按F9 可以设置断点调试。另外,其Command Window 面板也是一个很有用的功能,它能在代码断点停止时,在其中输入变量名并回车,便可看到此时变量的值;Command Window 面板甚至可以接受简单的JavaScript 命令。但Microsoft Script Debugger 调试器自身还存在一个bug 问题。

原文出自:http://www.cnblogs.com/xugang/archive/2010/08/05/1793392.html

PHP常见错误(二)

PHP常见错误(二)

mySQL Result Source Errors
mySql 资源错误
Warning: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in…
You need to take a look at the $result variable used to define the loop. More than likely there is a syntax error on the reported line before the $result field, or the value of $result does not exist.
通常,在进行msyql_query后,你需要对取得的数据进行遍历,如果出现上面错误时,你得检查一下你的$result是否是一个resource,这种情况在SQL查询语句错误(但错误没有被抛出)的情况下,所以,发现此问题时,可以考虑打印出SQL,到PHPMYDMIN里执行一下看看是怎么回事。

Supplied argument is not a valid stream resource…
This is usually caused when your code is looking for a table or other resource in the database that does not exist.
这个问题的情况通常是你代码中指定的表或者资源不存在于该数据库

Sessions are not being created or maintained
Sessions 不能被创建或者被修改维护
This can apply to any of the scenarios below:
这可能会出现以下几种情况
(1) The program isn’t remembering your login 程序没有记录下你的登录状态
(2) Your shopping cart won’t hold items 你的购物车没有保持你的商品状态
(3) Your php script is redirecting like crazy 你的PHP代码跳转的太疯狂
(4) “Call undefined function session_start” error 你的服务器没有打开Session功能
(5) PHP isn’t processing pages called by something like index.php?page=home&id=7 你的PHP代码不能打开某个页面,比如index.php?page=home&id=7

Your site is most likely dependent on register_globals. You can enable them by putting this line in your .htaccess (or just enabling in your php.ini if you have access):
你的网站可能会依赖register_globals(全局变量)打开。你可以将下面这行加到你的.htaccess文件里(当然,如果你有权限更改PHP.ini,还是改php.ini更方便)
php_flag register_globals On

Stream Errors
文件流错误
Warning: failed to open stream…
Warning: main(/index.php): failed to open stream: No such file or directory in…

This is usually because either the specified file is missing, or a file declared in a require() or include() function is missing. The easiest way to fix this is by re-installing the PHP program from a freshly-uploaded copy, or restoring the original config.php and just changing the db information. The include path may also be incorrect, but either way your script is looking for a file that isn’t there, or it is looking in the wrong place.
这两个问题的出现,通常是文件没有找到或者文件不能被require和include。(上面的英文我觉得不太准,个人认为应该是:先检查一下文件是否确实存在,检查文件的路径,当然,还得看一下php.ini的include_path,如果这些都检查过了。那么尝试用绝对路径加载文件)

Warning: fopen(…): failed to open stream: Permission denied in…
This is a permissions and/or ownership issue. Try first setting the permissions to 777 just to see if the script will run. If so, you should narrow down the permissions to 775. If not, set the user/group to user:nobody.
文件没有权限被读取,如果你想要你的文件被读取,设置文件权限为775,如果该文件需要被执行,那么就设置成777。否则设置用户权限/组为:nobody

Warning: <…> is not a valid stream resource…
Warning: fread(): supplied argument is not a valid stream resource in…
This is an error seen when trying to use functions like fopen(), fread(), feof(), etc. and are usually caused by an invalid or unavailable resource that is being called in the line specified. For instance, if the fread() function is returning this error, it could be that the file it is trying to access does not have the correct permissions or does not exist.
这也是一个常见错误,当你使用fopen(), fread(), feof()等函数时,就可能会出现,错误原因一般是fopen()或者其他函数打开文件失败,但错误信息被屏蔽,如$fp=@fopen('file.txt');,在这种情况下,再使用fread()读取时,就会出现上面的出错信息。

Warning: Failed opening….
Warning: Failed opening ‘…’ for inclusion (include_path=’.:/usr/local/lib/php’) in Unknown on line 0
Make the sure that the file mentioned (and its holding directorie) has read + execute permissions, and that the path to the file is correct. If not, you’ll need to add the path into the PHP code: (or .htaccess)
请确认文件是否有读写权限并且路径是正确的。如果不是,你需要把路径加载到你的PHP代码或者.htaccess文件里

include(”/path/to/files”);
Blank PHP Pages

You go to a .php page, but it’s blank.
The scope of what can cause blank pages is very broad, but there are a few things to look at:
-Is error reporting turned off anywhere in the script or in the .htaccess? If so, turn it on to see what is happening (php_flag display_errors on), or add the lines at the top of this page into the script.
-Is the PHP script even generating any output (usually you can tell my finding the print function?
-Check the database connection, i.e, username, dbname, user added to db, etc.
-Try using the full <?PHP ?> tags, rather than the shorter versions <? ?>
当你转向到a.php的页面时,但是打开的却是空白页面,出现这种问题的原因有很多,这里只介绍一些让你尝试着解决一下:
1、错误报告是否关闭了?如果是,请打开错误报告,并且在你的这个文件里加上一些输出试试
2、是否PHP代码本身就是一个输出页面,试着寻找一下print之类的函数?
3、检查数据库连接,例如,用户名密码等
4、试着用完整的PHP标签:<?php?>,而尽量少用短标签<??>


Also, if the software is prebundled (like phpBB or Gallery), then the index or one of the include pages could be corrupted. Usually you can just replace the problem page with a working version from another installation.
有时,一些软件的测试版本中往往都会存在着一个错误或者BUG等,你可以试着将出错页用该程序的其他版本来替换掉。象(phpBB或者Ballery)


Max Execution Time Error
最大执行时间错误
You receive some variant of a “Max_execution_time” error when loading a page.
This is caused when a PHP script takes longer to execute than the server allows, but can be adjusted by adding a PHP directive to your .htaccess: (in seconds, 0 = unlimited) or modifying the value in php.ini.
php_value max_execution_time 0
打开页面时,显示 "Max_execution_time" 执行时间错误。
这应该是你的PHP代码在运行的时候,超过了服务器所允许的时候,但是,这是可以通过修改.htaccess或者php.ini来进行修改(0代表了不限制)
php_value max_execution_time 0


Open_basedir Errors

Warning: Unknown(): open_basedir restriction in effect.
This is a protective feature of Apache that restricts PHP from accessing files/folders outside the user’s home directory. Most of the time this is due to an incorrect include path in one or more of the config files (which are usually mentioned). Look for something like this:
/includes/somefile.php
/admin/files/anotherfile.php
The heading / tells the filesystem that these folders are on the server root, and thus prevents PHP from accessing them. You can usually fix this by changing the path to these files to be absolute to their location:
/home/username/public_html/includes/somefile.php
or
./includes/somefile.php
出现Open_basedir问题的时候,往往需要先检查,PHP_INI里面有没有设定open_basedir,如果PHP.ini里没有设定,还需要查看apache的http.conf的虚拟主机设定里,也否也进行了设置。如果设定了,还需要检查这些目录是否可读写,否则会出现受保护的错误。