HOBALL 筆記簿

July 23, 2009

Javascript – catpuring mouse click

Filed under: Javascript — Tags: , , — hoball @ 1:00 pm

While developing my app, I wrote the protype using jQuery, but I only need one method from jQuery, which is click(). I wanted to build it with pure javascript, but have no idea how…(what a newbie!)

So I went to StackOverflow to ask my question, here is the permalink: www.stackoverflow.com

Summary of answers:

var p = document.getElementsByTagName("p");
for(var i=0; i<p.length; i++){
    p[i].onclick = function(){
        alert("p is clicked and the id is " + this.id);
    }
}

Breton also posted a fully featured suggestion of the jQuery click() method:

it looks a little bit like you miss more than just the click function of jQuery. You also miss jquery’s selector engine, chaining, and automatic iteration across collections of objects. With a bit more effort you can minimally reproduce some of those things as well.

var myClickCapture = function (selector) {
    var method, name,iterator;
    if(selector.substr(0,1) === "#") {
       method = "getElementById";
       name = selector.substr(1);
       iterator = function(fn) { fn(document[method](name));  };
    } else {
       method = "getElementsByTagName";
       name = selector;
       iterator = function(fn) {
          var i,c = document[method](name);
          for(i=0;i<c.length;i++){
             fn(c[i]);
       };
    };
    myClickCapture.click = function (fn){
         iterator(function(e){
            e.onclick=fn;
         })
    } 

    return myClickCapture;

}

I haven’t tested the code, but in theory, it gets you something like this:

myClickCapture("x").click(function(e){ alert("element clicked") });

Hopefully this gives you a sense of the sorts of things jquery is doing under the covers.

Advertisements

July 22, 2009

Javascript fix for the failure of IE to pass HTTP_REFERER in setting new URL on location.href

Filed under: Javascript — Tags: , , , , — hoball @ 12:49 pm

Setting a new URL using location.href will change the HTTP_REFERER on Firefox/Opera/Safari, but NOT on IE

Here is the hack to fix this (quoted from here):

/* location.href FIX for MSIE */
//http://blogs.msdn.com/ie/archive/2008/10/30/hot-off-the-press-codefocus-on-ie8.aspx#9028128
function setHref(url) {
    var isIE = (navigator.appName.indexOf("Microsoft")!=-1) ? true : false;

    if (!isIE) {
        //Standards based browsers
        parent.location.href = url; // Set target: self. / parent. / top.
    } else {
        var lha = document.getElementById(‘_lha’);
            if(!lha){
                lha = document.createElement(‘a’);
                lha.id = ‘_lha’;
                lha.target = ‘_parent’;  // Set target: for IE
                document.body.appendChild(lha);
            }
            lha.href = url;
            lha.click();
    }
}

October 31, 2008

Javascript innerTEXT innerHTML

Filed under: Javascript — Tags: , , , — hoball @ 2:26 pm

一圖勝萬字解釋

October 21, 2008

悟透JavaScript

Filed under: Javascript — Tags: , — hoball @ 12:52 pm

轉載李戰(leadzen)自http://www.cnblogs.com/leadzen/archive/2008/02/25/1073404.html

引子

編程世界裡只存在兩種基本元素,一個是數據,一個是代碼。編程世界就是在數據和代碼千絲萬縷的糾纏中呈現出無限的生機和活力。

數據天生就是文靜的,總想保持自己固有的本色;而代碼卻天生活潑,總想改變這個世界。
(more…)

Blog at WordPress.com.