[转]javascript中判断变量是否为array

javascript中要判断一个变量是否为array通常是比较困难的,因为

通常的做法是判断

可是这个方法有一个问题,如果一个数组是来自另一个frame中的,那么它的constructor 将是另一个对象。
YUI中用了如下的方法:

简单来说,就是判断这个变量有没有 length 属性,同时有没有 splice 方法。可惜,这样的判断方法是可以用一个 object 来伪造数组的。
Google 的 Mark Miller 发现,用下面方法,可以简单又可靠的得到一个变量是否一个数组:

转自:http://stauren.net/log/fqibpxyz3.html

更新一下我的jlUtil

修正了几个BUG,已经在实际项目中使用,没有问题。

Array.indexOf in Internet Explorer

According to this document at Mozilla Developer Center, Javascript 1.5 has been implemented in a browser since at least the first releases of Mozilla as open source browser, which means, in other words, since around 1998. Let's assume it was 2002 which is marked as the release of the 1.0 version.
And I was doing some interface programming lately and I needed to check if an element was in an array. Went to Gecko's documentation (that's the one I normally use, since it's less overbloated with crappy ads) and checked that Array objects had an indexOf function. Cool! I used that function on my code. Then once I finished with all the development, went to The Horror (i.e. Internet Explorer) and tested it.
Surprise! It was broken! What had I done? (You know the debugger for Internet Explorer is not specially helpful)
I suspected of the indexOf function, and recalled vague memories about doing a custom search function in the past for looking into arrays and not having to write a for(i=0; i<ar.length;i++) thing each time…
Mmmm… did a alert('Array.indexO') in ie and what did I get?: undefined
So they have been spent five years for releasing ie7 and still they didn't implement Array.indexOf!
No worries, though. Javascript is flexible! Look, IE, I don't care if you choke on the mere seeing of indexOf, you're going to run it whether you like it or not!

and voila! my script wasn't broken anymore!

jlUtil For Javascript

昨天用js写了一个Json_encode的工具函数,附上模仿jQuery并加强的extend函数。
对于extend函数,主要加强的是可以遍历子对象来extend,而不是简单的覆盖。
getJSON的使用方法:

extend的使用方法:

下面是代码:

[转]Web 设计与开发者必须知道的 15 个站点

转一篇cb的好文,以后应该会用到。
公司博客整整一个月没有更新了,最近一段时间,全公司都忙于两件事,为海尔集团做定制,为一个合作伙伴做 OEM,终于有了眉目。工作期间,常用到一些工具与帮助站点,今天读到一篇文章,介绍了15个对 Web 设计与开发师极端有用的站点,里面有不少也是我们一直在使用的,也许对很多人都有用,翻译出来以饷同仁。 ColorCombos

配色是 Web 设计的首要大事,Color Combos 帮你预备了数千种配色方案,可以根据色调浏览选取。
LIpsum

Lorem Ipsum Text 是一中自造的,字典中不存在的单词,是在演示页面结构的时候,需要加文字的地方使用 Lorem Ipsum Text 填充可以避免用户因关注文字的意思而分神。Lipsum.com 可以帮你生成制定数目的 Lorem Ipsum 单词。
What the font?

有时候你对某个 Logo 感兴趣,想知道是拿什么字体做的,可以将 Logo 图片上传到这个网站,结果之准确会让你难以置信。
ConvertIcon

Favicon 越来越受欢迎,尤其随着 Firefox 的流行,Firefox 会将你站点的 Favicon 标志显示在标签上,也会显示于书签,Favicon 支持多种图形格式,比如 png,但 IE 并不支持 png,该站点可以帮助你将 png 等图片格式的 Favicon 转换成 IE 支持的 ico 格式。
BgPatterns

现代 Web 设计的趋势之一包括使用背景图案,BgPatterns.com 可以帮你设计背景图案,他们有大量可选的图案,可以按不同方式接合,还可以设置透明度。
HTML Encoder

如果你要在网站中显示 Web 代码,那将是非常烦人的事,你必须将诸如 < 一类的符号用编码表示,这个网站可以帮你做这些事情,只需将你的代码复制进去,他们会帮你生成可以直接使用的代码。还有 HTML Decoder 帮你做相反的事。
Xenocode Browsers

该网站非常有用,可以直接在站点中预览你的网站在 IE6, IE7, IE8, Firefox 2, Firefox 3, Google Chrome 以及 Opera 中的样子。唯一的不足是,不支持 Mac 和 Linux 环境下的浏览器。
Test Everything

这个网站包含了超过 100 中工具帮你测试几乎所有东西,如跨浏览器兼容,查 PR 值,甚至帮你验证代码。
Sprite Generator

CSS Sprites 在显著降低 HTTP 请求方面功不可没,但 CSS sprite 可不是个简单的技术,Sprite generator 可以帮你做这些繁复的工作,将你的图片打包成 zip 上传上去,他们会把你的图片组合成 sprite,还帮你生成 CSS 代码。
Buttonator

这个网站可以帮你设计漂亮的按钮。
Load Impact

这个压力测试站点可以帮你测试你的站点的抗压能力,如果你的某篇文章曾经上过 Digg 或 StumbleUpon 等网站的首页,你就会知道抗压测试多么重要。
IconFinder

这个网站收藏了大量来自网络的免费图标。
TypeTester

该站点可以帮你测试不同字体的显示效果。
CSS Tidy

这个站点可以帮你检查 CSS 代码,并修正其中的错误,比如,它可以发现你的 CSS 代码中最常见到重复设置问题。
Contact Forms Generators

自定义表单可以帮助你实现同访问者的互动,无需编程,就能快速生成访客反馈表单,甚至帮你生成邮件发送 PHP, ASP, Perl 代码。
本文来源:http://www.catswhocode.com/blog/15-sites-web-developers-and-designers-should-know
中文翻译来源:COMSHARP CMS 官方网站

关于Javascript

最近对JS非常感兴趣,重新拿起jQuery作者John Resig写的《Pro Javascript Technology》的电子书来看,发现有点麻烦,得放到PSP里面看,于是到当当和卓越上搜了一下,发现居然没有中文译本。于是萌生了翻译这本书的想法,我没有做过翻译的工作,更没有翻译过书,只是最近对Javascript很感兴趣,不知道自己能不能坚持下来。
如果有fans来关注,应该可以坚持下来,哈哈
不过希望我的翻译不要引起版权纠纷。

Javascript Timeout Hacks

Our first user script simply displays an alert saying “Hello world!” when it is executed.

Example: Display the “Hello world!” alert

Although this code looks obvious enough, and does exactly what you would expect, Greasemonkey is actually doing a number of things behind the scenes to ensure that user scripts do not interact badly with other scripts defined by the original page. Specifically, it automatically wraps your user script in an anonymous function wrapper. Ordinarily you can ignore this, but it will eventually creep up and bite you in the ass, so you may as well learn about it now.
One of the most common ways this can bite you is that variables and functions that you define in a user script are not available to other scripts. In fact, they are not available at all once the user script has finished running. This means that you will run into problems if you are expecting to be able to call your own functions later by using the window.setTimeout function, or by setting string-based onclick attributes on links and expecting Javascript to evaluate your function names later.
For example, this user script defines a function helloworld, then attempts to set a timer to call it one second later.

Example: Bad way to delay calling a function

This will not work; no alert will be displayed. If you open JavaScript Console, you will see an exception displayed: Error: helloworld is not defined. This is because, by the time the timeout expires and the call to helloworld() is evaluated, the helloworld function no longer exists.

If you need to reference your user script's variables or functions later, you will need to explicitly define them as properties of the window object, which is always available.

Example: Better way to delay calling a function

This works as expected: one second after the page loads, an alert pops up proudly displaying “Hello world!

However, setting properties on window is still not ideal; it's a bit like using a global variable when a local one will do. (Actually, it's exactly like that, since window is global and available to all scripts on the page.) More practically, you could end up interfering with other scripts that were defined on the page, or even other user scripts.
The best solution is to define an anonymous function yourself and pass it as the first argument to window.setTimeout.

Example: Best way to delay calling a function


What I'm doing here is creating a function without a name (an “anonymous function”), then immediately passing the function itself to window.setTimeout. This accomplishes the same thing as the previous example, but it leaves no trace, i.e. it's undetectable to other scripts.
I find that I use anonymous functions regularly while writing user scripts. They are ideal for creating “one-off” functions and passing them as arguments to things like window.setTimeout, document.addEventListener, or assigning to event handlers like click or submit.

略懂 HTML 的朋友都知道,如果想让一个链接在新窗口中打开,通常的做法是利用 target=”_blank” 来设定 a 标签。例如:

  1. <a href="http://jennal.cn" target="_blank">Jennal.cn</a>

这种做法确实比较方便,但在 XHTML 1.0 Strict 中去掉了 target 属性,也就是说我们不能再利用 target 属性来控制链接的行为。虽然当今流行的浏览器在 XHTML 1.0 Strict 甚至 XHTML 1.1 下扔能正确执行 target=”_blank”,但这样的代码毕竟是不规范的,不推荐使用。
很自然,我们会想到用 Javascript 来解决这个问题,我通常是使用下面的方法:

  1. <a href="http://jennal.cn" onclick="window.open(this.href);return false;">Jennal.cn</a>

这样虽然可以满足要求,但是当链接很多的时候,代码就显得有些臃肿了。为了简化代码,我们应该用 DOM Event / DHTML 的方法来解决这个问题。今天我恰好在做一个网页,需要使用 XHTML 1.0 Strict,准备自己写一个这样的 Javascript。不过幸好我养成了万事先 Google 的坏习惯,还真让我找到了一个很完善的 Javascript 弹窗代码,这段代码不仅写的漂亮,通用性强,而且还考虑到了当今流行的浏览器按下组合键点击链接的情况,已经非常完善了。代码源头在这里,作者提供了源码下载和一个演示
这段代码的通用性非常强,作者原文中举例写的也很详细,其实最简单的用法就是为需要开新窗的链接添加 rel=”external” 属性,当然,你也可以自己定制根据 class 或其它什么属性来判断。

  1. <a href="http://jennal.cn" rel="external">Jennal.cn</a>

当然,在网页设计中,弹出新窗口在多数情况下应该尽量避免,只在可以提高用户体验的情况下才需要使用。此外,由于有些 Pop Window Blocker 会拦截 Javascript 弹出窗口,我们可以修改这段代码,通过判断窗口是否成功建立来给出关闭弹窗过滤的提示,相信可以使用户体验提升不少。
转自:http://blog.istef.info/2007/05/17/open-a-new-window-when-click-a-link-under-xhtml-10-strict/

Javascript 刷新页面的几种方法

1    history.go(0)
2    location.reload()
3    location=location
4    location.assign(location)
5    document.execCommand('Refresh')
6    window.navigate(location)
7    location.replace(location)
8    document.URL=location.href