Posts Tagged ‘javascript’

Javascript addEventListener


Since I have been thinking about my image gallery project, I have a lot
of things to study for the best work.

Today, I read the article from http://www.quirksmode.org, it mentions to
“addEventListener” method in Javascript.

addEventListener is the method which used to add an event to an element.

Suppose if you want to add the alert event when user clicks on an element,
just do like this.

element.addEventListener('click', function(){ alert("Something"); }, true);

So, if you click on an element, alert window will appear.

But in IE, you can’t use “addEventListener” to add an event to element, it uses
“attachEvent” to add an event.

In IE, the above code won’t work, use the below code.

element.attachEvent('onclick', function(){ alert("Something"); });

It’s complicated to write code for both IE and other browsers right?
So, I have written the simple function to add an event to an element
that works on both.

And here is my function.

var zEvent = {
      'add' : function(el, eventName, fn){
            if(navigator.appName == "Microsoft Internet Explorer") el.attachEvent('on' + eventName, fn);
            else el.addEventListener(eventName, fn, true);
      },
      'remove' : function(el, eventName, fn){
            if(navigator.appName == "Microsoft Internet Explorer") el.detachEvent('on' + eventName, fn);
            else el.removeEventListener(eventName, fn, false);
      }
};

This is how to use it.

zEvent.add(element, 'click', function(){ alert("Something"); });

Hope this useful for you.
Thanks.


ที่มา (Backgrounds).

เมื่อวาน มีสมาชกในโซนไอทีคนนึงขอให้ผมช่วยทำหน้าโฮมเพจให้ใหม่
(Yesterday, the member from #zone-it asked me to help him to make the new home page.)
และเค้าก็บอกว่าอยากได้ slide แบบในหน้าแรกของโซนไอที
(And he told me he want the slide in #zone-it homepage on his homepage.)

ตอนแรกผมก็คิดๆ ว่าจะทำยังไงให้ง่ายๆ เนื่องจากขี้เกียจจะบรรยายโค้ดให้ยาว
(In the beginning, I have no idea about this because I don’t want to write the long code.)
ผมก็เลยไปมองๆ หาตัวช่วย แล้วก็เจอ jQuery
(So, I searched the javascript framework, then I found jQuery.)
ก็เลยนั่งอ่านๆ docs มันซักพักนึง เห็นว่าน่าจะง่ายสำหรับงานนี้
(I read its docs and I thought jQuery will make me easy to do this job.)
แล้วก็ลงมีทำ
(And I started writing code.)

สุดท้ายก็ได้เจ้านี่มา http://zn.in.th/labs/adverslide/
(At the end, I got this http://zn.in.th/labs/averslide/.)

Please never mind above passage, I just tried to write in English and translate into Thai.
But please read this below passage.

Today, I first used jQuery to write my some javascript code, and I come with this “Adverslide”.
“Adverslide” is the another slide show write on javascript using jQuery, it contains 5 pages to slide.

When you click on the number at the right, the slide show will slide (animation) to that page.

I haven’t tried it on IE6, 7 and another browsers. I only tried on Firefox 3.6.

Finally, if you want to try this slide show, please visit at http://zn.in.th/labs/adverslide/


Travian select all messages


วันนี้เข้า ทราเวียน เข้าไปเช็คข้อความที่โดนตี (รวมถึงจากกิลด์ด้วย) มันช่างเยอะเสียเหลือเกิน
อยากจะลบมากแต่ต้องมานั่งติ๊กทีละอัน (ไม่เข้าใจว่าทำไมมันไม่ทำปุ่ม Select All)

ด้วยความขี้เกียจก็จัด javascript for loop ง่ายๆ ชุดนึง แล้วก็ ENTER!! กดลบ จบ!

นี่ Script
javascript:for(i=1;i<=10;i++){varrn='n'+i;void(document.getElementsByName(rn)[0].checked=true);}

Bookmark ไว้ซะ!