Dec 08, 2006

document.lazy_writerをバージョンアップ

先日書いたdocument.lazy_writerをバージョンアップしました。
http://la.ma.la/misc/js/lazy_writer/

ロードするスクリプトのURLが動的になっているケースも多いので、
- URL指定の文字列を部分一致に。(完全一致するパターンがある場合はそちらを優先)
- 正規表現でパターンを指定できるように
- 関数でパターンを指定できるように

しました。

以下サンプル。

// 適用するURLを正規表現か部分文字列で指定できます。
document.lazy_writer(/google/, function(str){ /* your custom document.write */ });
document.lazy_writer("google", function(str){ /* your custom document.write */ });


他の条件を絡めたいときは、関数で実行条件を指定することが出来ます。
// 今見ているページのURLが.htmlで終わっていれば適用する
document.lazy_writer(
    // 実行するか判別するための関数
    function(url){ 
        return (
            url.indexOf("google") != -1
            && /\.html$/.test(location.href)
        )
    },
    // 実行する関数
    function(str){/* your custom document.write */}
)

// 常にalertを使って出力、デバッグ用
document.lazy_writer(
    function(url){return true},
    function(str){alert([this.current_script.src, str])}
)

という具合。個人的に必要な機能は一通りそろったと思うので開発終了。
Posted at 10:22 | WriteBacks (5) | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.
















A quick preview will be rendered here when you click "Preview" button.