Featured Posts

CentOS LAMP Setup 很土鳖的问题,浪费我几个小时,终于搞定! 在 CentOS 下使用 xampp 的集成套件搭建 LAMP 环境,启动 Apache 后,用浏览器访问 web 程序,居然提示下载源文件!! 也就是...

Readmore

看看我写的 GFW 小故事 我是一个乖孩子,喜欢上网跟朋友们聊天、玩游戏 警察叔叔说,网上很黄很暴力,我却很傻很天真,不让我再看到我最爱的网站 直到有一天,一个无辜的小鸟被无情地封杀,我才意识到问题的严重性 面对河蟹坚硬的钳子,小鸟不能改变什么,唯一能做的,就是送上几句脱口秀解解气 据说那个墙,又黑又高,看到朋友们发来的结构图,我哭了 我再也看不见可爱的...

Readmore

Apache ReWrite QUERY_STRING 问号 ? 看一条应用中简单的 rewrite 规则: 将请求: http://www.leakon.com/soft/install?ver=2.0 rewrite 为: http://www. leakon.com/my/soft/install.php 配置文件 httpd.conf...

Readmore

  • Prev
  • Next

Iframe Tips ABC

Posted on : 24-07-2010 | By : leakon | In : JavaScript

0

通常我们用 js 脚本创建 iframe 时,会这样写:

var iframe = document.createElement('iframe');

之后我们可能会定义 id、name、border 等属性,这些看似简单,其实 IE 与非 IE 浏览器之间、IE 和 IE 高版本之间的迥异,使得 iframe 的相关兼容性操作非常地有文章。

牛A:frameborder

现象:使用 (iframe.frameBorder = 数值) 或(iframe.setAttribute(‘frameborder’,数值)) 在 IE 浏览器下无效
原因:IE 浏览器区分属性名称大小写
解决方法:iframe.setAttribute(‘frameBorder’,'0′) Or iframe.setAttribute(‘frameborder’,'0′,0),兼容所有浏览器。

注:经测试,IE8已经修复此问题

牛B:动态将Form target到iframe

背景:假设现在我们要让一个 Form 表单结果提交到一个 HTML 结构中已存在的 iframe,会这样做:

<form id="form" name="form" target="相应iframe的name:iframeNB" method="post" >
</form>
<iframe name="iframeNB" ></iframe>

OK,什么问题也没有,再假设我们需要提交到脚本动态生成的 iframe 中,会这样做:

<form id="form" name="form" target="iframeNB" method="post" >
</form>
<script>
var iframe = document.createElement('iframe');
iframe.name = 'iframeNB';
...
someParent.appendChild(iframe);
</script>

去 IE 浏览器里试试,你会发现 Form 会在新窗口显示提交结果,Why?
原因:
我为此尝试了很久,结果是IE此前版本不能通过(iframe.name=)这种方式给 iframe 设置 name 值,也就是说生成的 iframe 是没有 name 值的,但却可以 alert 出来,这很诡异;当然,这也并不是没有解决办法。
解决方法,为此我们得为 IE 单独写一行代码:

  /*only for ie */ 
    var iframe = document.createElement('<iframe name="iframeNB">'); 

看到这行代码,我们笑了,这是天大的杯具(喜剧?)~~不管IE有多么搓的问题,他总会有自己一套解决之……
而且这行代码会在其他非 IE 浏览器抛出异常,所以我们可以利用这点来做最终版:

    var iframe; 
    try{ 
        iframe = document.createElement('<iframe name="iframeNB">'); 
    }catch(e){ 
        iframe = document.createElement('iframe'); 
    } 
    iframe.name = 'iframeNB'
    ... 
    someParent.appendChild(iframe); 
    ... 

[2009-12-9]补充:最佳实践 – YUI 是如何 creat iframe 的

    /** 
    * @description Creates an iframe to be used for form file uploads.  It is remove from the 
    * document upon completion of the upload transaction. 
    * @method createFrame 
    * @private 
    * @static 
    * @param {string} optional qualified path of iframe resource for SSL in IE. 
    * @return {void} 
    */
 
    function _createFrame(secureUri){ 
    // IE does not allow the setting of id and name attributes as object 
    // properties via createElement().  A different iframe creation 
    // pattern is required for IE. 
    var frameId = 'yuiIO' + this._transaction_id,io; 
    if(YAHOO.env.ua.ie){ 
        io = document.createElement('<iframe id="' + frameId + '"name="' + frameId + '" />'); 
        // IE will throw a security exception in an SSL environment if the 
        // iframe source is undefined. 
        if(typeof secureUri == 'boolean'){ 
            io.src = 'javascript:false'
        } 
    }else{ 
        io = document.createElement('iframe'); 
        io.id = frameId; 
        io.name = frameId; 
    } 
    io.style.position = 'absolute'
    io.style.top = '-1000px'
    io.style.left = '-1000px'
    document.body.appendChild(io); 
    YAHOO.log('File upload iframe created. Id is:' + frameId, 'info''Connection'); 
    } 

这里需要额外注意到的一点是:

// IE will throw a security exception in an SSL environment if the
// iframe source is undefined.
if(typeof secureUri == 'boolean'){
io.src = 'javascript:false';
}

姑且算是牛D吧 =.=!

牛C:iframe.onload

关于 onload 这点大家可以参考怿飞师父的文章:判断 iframe 是否加载完成的完美方法,在此纯引用一次代码:

var iframe = document.createElement("iframe"); 
iframe.src = "http://www.planabc.net"
if (iframe.attachEvent) { 
    iframe.attachEvent("onload"function () { 
        alert("Local iframe is now loaded."); 
    }); 
} else { 
    iframe.onload = function () { 
        alert("Local iframe is now loaded."); 
    }
} 
document.body.appendChild(iframe); 

需要注意到的是:

  • IE8也不支持iframe.onload
  • Opera两者均可,所以使用此方法会绑定前者
  • 即使我们不预设iframe.src = some urls,也会默认执行一次onload事件,可以通过分析 src 规避。

附测试文件:
1.iframe_ie_ugly.html
2.iframe_fixed.html
以上,我认为 ABC 中最牛的就是 B 了,这也是我标题的亮点=.=! 斯密达们认为呢?

上述文字转自 http://blog.silentash.com/2009/12/iframe-tips-abc/,感谢原作者给我提供的巨大帮助。

转载在这里与更多人分享。

IE Bug Javascript

Posted on : 12-07-2010 | By : leakon | In : JavaScript

0

问题描述:
当 A 标签的文本是 http 开头时,设置 href 属性,会同时以相同的值更新链接文本,反之亦然。

解决方法:
在设置 href 时,在开头添加一个空格 ' ',即可解决此 bug。

测试 Demo:

http://code.leakon.com/javascript/set_href/set_href.html

注意,只在 IE 下会出现错误。

预期的功能是修改链接的 href 属性。

使用默认的修改方法有 bug,会把链接文本改掉!

0x80040111 NS_ERROR_NOT_AVAILABLE

Posted on : 08-07-2010 | By : leakon | In : JavaScript

0

Firefox 在处理 Ajax 遇到错误时会报 0x80040111 异常。

在网上查了下,有的说法是,在很短的时间间隔发送了 2 次 Ajax 请求,XMLHttpRequest 的值还没有返回回来就又被请求了一次。

还有一个情况,是我遇到的,在请求成功返回后执行回调函数时,引用了空对象的属性,debug 报错:"null has no properties"。

目前只遇到这 2 种情况,记录备忘。

WinSCP UltraEdit Unix DOS Format

Posted on : 10-06-2010 | By : leakon | In : 非技术

0

editor_transfer

用 WinSCP + UltraEdit,可以直接修改服务器上的文件,保存时自动上传到服务器上,用起来很方便。

最近发现个情况,服务器上的文件不管是 DOS 还是 Unix 格式,用 UltraEdit 打开总是显示 DOS 格式,保存后,却都变成了 Unix 格式!

反复检查了 UltraEdit 的设置选项,都没问题,编辑本机文件都是正常的。

后来开始怀疑 WinSCP。

费了好半天劲,终于找到原因,在 Editor 选项中可以设置外部编辑器,然后设置编辑器选项,如顶图所示红框的位置。

意思是,用编辑器传文件强制使用文本方式传输,这就是导致问题的关键点。

把这个选项去掉,打开文件格式可以正确识别,保存后格式也不会全部转换成 Unix 了。

记录 & 分享。

KB927917 IE 报错 解决方法

Posted on : 03-06-2010 | By : leakon | In : Browser, JavaScript

0

在 body 标签内调用 append 方式给 body 增加一个节点,如果此时 body 未加载完,也就是 body 标签还未关闭,IE 将报错:KB927917。

页面错误提示:Unable to modify the parent container element before the child element is closed.

查了下资料,知道了问题的产生原因:

首先是微软的详细解释:http://support.microsoft.com/kb/927917

最后发现其实微软的MSDN上早已列举了解决办法:
http://blogs.msdn.com/ie/archive/2008/04/23/what-happened-to-operation-aborted.aspx

官方给出的解决办法如下:

  1. Moving your script execution to a function that is invoked after parsing is complete (e.g., onload)
  2. Adding the defer boolean attribute to the script block (this defers execution of the script content until parsing is complete)
  3. Limiting your tree modifications to the script-element's immediate parent
  4. Moving the location of your script block to a child of the body (this usually solves most problems, while allowing the most flexibility in terms of scenarios).

接到骗子来电 85928807

Posted on : 12-05-2010 | By : leakon | In : 默认分类

0

刚接到一个电话:+000196852202,说我的工商银行信用卡在新世界消费9000多,让我尽快去银行缴费。有问题致电85928807,跟崇文区XXX行联系。

你MLGB,哥没有工行信用卡,你个SB。

骗子,各位小心!

mysql 持久连接 mysql_connect mysql_pconnect

Posted on : 12-05-2010 | By : leakon | In : MySQL

0

在某些场合,mysql_pconnect( ) 是不适用的。

——————————————————————————–

状况一:

使用 1 部 web server 与 1 部 MySQL server(两者可能同在一部主机上),而 web server 固定只对 MySQL server 上的某一个数据库进行存取动作。

因为每次存取数据库时,都是由 web 那边使用同一账号对 MySQL 上的同一数据库作业,若我们将 MySQL 与 web server 的「同时联机数」都调整为 200,就好像 MySQL 这边一直有 200 位「服务生」,随时等着接待来自 web 的 200 位「顾客」似的。而且「顾客」离开之后,「服务生」也不下场休息,时时都站在门口等着接待下一个「顾客」。

在这种情况下,您只要注意将 MySQL 的「同时联机数」调得比 web server 的高或相等,就会发现使用 mysql_pconnect( ) 是个不错的选择。
——————————————————————————–

状况二:

使用 1 部 web server 与 1 部 MySQL server(两者可能同在一部主机上),而 web server 会对 MySQL server 上的两个数据库进行存取动作。

从 web server 那边提出数据存取需求时,有时是针对第 1 个数据库(DB1),有时则是针对第 2 个数据库(DB2)。若我们也将 MySQL 与 web server 的「同时联机数」都调整为 200,这样一来,就好像 MySQL 这边有 200 位「服务生」,但同时经营两个「吧台」(DB1 与 DB2),而「顾客」可能多达 200 位。

一开始,DB1 这个「吧台」比较热门,MySQL 派了 150 位「服务生」上场接待;同样地,当「顾客」离开之后,这 150 位「服务生」仍守着 DB1 而不下场休息。后来,DB2 那边也热闹起来了,「顾客」越来越多,MySQL 得加派「服务生」上场,有几个能派?答案是 50 个!

为什么「服务生」的人力调配会捉襟见肘?那是因为 web 那边使用了 mysql_pconnect( ) 来建立联机。「服务生」一开始被指定到哪个「吧台」工作,就会持续在那边停留,绝不「转台」。
——————————————————————————–

请注意,当使用持续性的联机时,每个已建立的联机只为来自同一部 web server、使用同一组账号,且存取同一数据库的使用者服务。

如此一来,假设每部 web server 的「同时联机数」都是 200,而且同时使用 2 部 web server 会怎么样呢?从 web1 来了 50 个「顾客」,先是到 DB1 走一趟,接着再到 DB2 晃一圈,这样需要多少「服务生」接待他们?100 个(web1->DB1: 50 web1->DB2: 50)!又从 web2 来了 50 个「顾客」,也做了同样的动作(web2->DB1: 50 web2->DB2: 50)。在此之后,还有「服务生」是闲着的吗?后续若从 web1 或 web2 同时涌入多于 50 位「顾客」时,谁来应付他们?

倘若您使用的是像 Apache 这类的 multi-process web server(一个 parent process 协调一组 children processes 运作),某个 children process 建立的「持续联机」,是不能分享给其它 children process 来使用的(「服务生」只对先前接待过的「顾客」服务)。在这样的情况下,将会使得 MySQL 上闲置的 process 越积越多(很多「服务生」站在门口等着「老顾客」上门,而不理会「新顾客」)。
mysql_pconnect( ) 一定是最佳选择吗?我想未必尽然。
——– 两者之间的区别 ————–
mysql_pconnect() 和 mysql_connect() 非常相似,但有两个主要区别。
首先,当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接

标识而不打开新连接。
其次,当脚本执行完毕后到 SQL 服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close() 不会关闭由

mysql_pconnect() 建立的连接)。
可选参数 client_flags 自 PHP 4.3.0 版起可用。
此种连接称为”持久的”。
看到这里,写一条代码来测试一下

/*
* pconnect_test.php
*/

$link = mysql_pconnect("localhost", "mysql_user", "mysql_password")
or die("Could not connect: " . mysql_error());
print ("Connected successfully");

通过刷新网页的方式执行这条代码,发现每执行一次,mysql的进程数就增加一个。在这里我不禁有了疑问。上面说mysql_pconnect这个函

数的使用的时候,不是说”当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到

,则返回此标识而不打开新连接”么?为什么我每刷新一次页面他就给我打开一个新的连接呢?

考虑到这有可能是PHP的bug,我到PHP的bug列表中找关于和too many connections 有关的条目。
相关的话题主要有三个,分别是

#11966        mysql_pconnect opens new connections with the same parameters
#26117        Persistent connection not reused
#13589        Persistent connections stay open and accumulate

描述比较长,我就不在这里贴,具体的内容你自己去看。重点主要是”当一个进程打开一个mysql的持续连接,只要该进程还存在,这个持续

的连接就不会断开,而且每一个进程会打开一个mysql的持续连接,而不能使用其他进程打开的持续连接”。

MySQL IN 条件语句 排序

Posted on : 08-05-2010 | By : leakon | In : MySQL

0

有个场景,一个几万条记录的表,主键是 id,我想从表中取 id 为 30,20,80,40 的几条记录。

注意,30,20,80,40,是我预期的顺序,我希望 MySQL 按这样的顺序返回记录。

于是我这样写 SQL:

SELECT * FROM my_table WHERE id IN (30, 20, 80, 40);

结果是,他没有按我给的顺序返回。

怎么办?

查到了 FIELD() 函数。

FIELD(str,str1,str2,str3,...)
Returns the index (position) of str in the str1, str2, str3, ... list. Returns 0 if str is not found.

把 SQL 语句改写为:

SELECT * FROM my_table WHERE id IN (30, 20, 80, 40) ORDER BY FIELD(id, 30, 20, 80, 40);

排序过程是:

把选出的记录的 id 在 FIELD 列表中进行查找,并返回位置,以位置作为排序依据。

这样的用法,会导致 Using filesort,是效率很低的排序方式。除非数据变化频率很低,或者有长时间的缓存,否则不建议用这样的方式排序。

把 MySQL 返回的结果,用 PHP 在内存中按 id 顺序重新排列,是个不错的优化方案。

sudo: sorry, you must have a tty to run sudo

Posted on : 03-04-2010 | By : leakon | In : Linux

0

I was recently working on a Perl script that would SSH to another server and run a sudo command on the remote server that was failing. The error that was received is below.

Error: sudo: sorry, you must have a tty to run sudo

The reason for this is an update along the way with sudo locked it down further by adding the below line to /etc/sudoers configuration file.

Defaults requiretty

To allow a remote script to login and run a command via sudo simply comment out that line as shown below.

# Commented out so remote script can login and run a command without a tty
# Defaults requiretty

I would suggest making a comment in the sudoers file along with the actual script that is running just in case there is another systems administrator that is tasked with working on this server at a later date. Now when your script runs it will not throw that error and should be able to run the remote command that was initially required.

MySQL 索引优化

Posted on : 24-03-2010 | By : leakon | In : MySQL

2

表中包含 10 万条记录,有一个 datetime 类型的字段。

取数据的语句:

SELECT * FROM my_table WHERE created_at < '2010-01-20';

用 EXPLAIN 检查,发现 type 是 ALL, key 是 NULL,根本没用上索引。

可以确定的是,created_at 字段设定索引了。

什么原因呢?

用 SELECT COUNT(*) 看了一下符合 WHERE 条件的记录总数,居然是 6W 多条!!

难怪不用索引,这时用索引毫无意义,就好像 10 万条记录的用户表,有个性别字段,不是男就是女,在这种字段设置索引是错误的决定。

稍微改造一下上述语句:

SELECT * FROM my_table WHERE created_at BETWEEN '2009-12-06' AND '2010-01-20';

这回问题解决!

符合条件的记录只有几百条,EXPLAIN 的 type 是 range,key 是 created_at,Extra 是 Using where 。

自己总结个准则,索引的目的就是尽量缩小结果集,这样才能做到快速查询。