Tag Archives: JavaScript

[jQuery] ブラウザの描画領域のサイズを取得する

このエントリーをはてなブックマークに追加
はてなブックマーク - [jQuery] ブラウザの描画領域のサイズを取得する
Share on Facebook

winHeight = $(window).height();
winWidth = $(window).width();

[その他] Poderosaマクロ – telnet自動ログイン

このエントリーをはてなブックマークに追加
はてなブックマーク - [その他] Poderosaマクロ – telnet自動ログイン
Share on Facebook

  • ワンクリックでtelnetログインし、自動でID/PWを入力するマクロ
  • いじるファイル: Poderosaインストールディレクトリ > Macro > AutoLogin.js
  • 登録方法: [ツール] > [マクロ] > [環境設定] > [新規] で上記ファイルを指定
//AutoLogin.js
import Poderosa;
import Poderosa.ConnectionParam;
import Poderosa.Terminal;
import Poderosa.Macro;
import Poderosa.View;
import System.Drawing;
import System.Threading;

var vars = new Object();
// telnetの場合
connect("ホスト名", ConnectionMethod.Telnet, 23, EncodingType.UTF8, "ログインID", "ログインPW");
// SSHの場合
//connect("ssh-host", ConnectionMethod.SSH2, 22, EncodingType.EUC_JP, "ログインID", "ログインPW");

function connect(host, method, port, encoding, id, password) {
    vars.env = new Environment();
    if (method == ConnectionMethod.Telnet) {
        vars.param = new TelnetTerminalParam(host);
    } else {
        vars.param = new SSHTerminalParam(method, host, id, password);
    }
    vars.param.Port = port;
    vars.param.Encoding = encoding;
    vars.connection = vars.env.Connections.Open(vars.param);
    if (method == ConnectionMethod.Telnet) {
        wait("login: ");
        sendln(id);
        wait("Password: ");
        sendln(password);
    }
}

function sendln(s) {
    vars.connection.TransmitLn(s);
}

function wait(s) {
    Thread.Sleep(10);
    var r = vars.connection.ReceiveData();
    while(r.indexOf(s) == -1) {
        Thread.Sleep(10);
        r += vars.connection.ReceiveData();
    }
}

[jQuery] セレクタで「<」「>」が選択できない

このエントリーをはてなブックマークに追加
はてなブックマーク - [jQuery] セレクタで「<」「>」が選択できない
Share on Facebook

症状

JavaScriptにおいて、「<」「>」は基本的にダメ文字(使用不可)だが、jQueryのセレクタ内で使用すると選択できたりできなかったりすることがある。

原因

そもそも、XHTMLの仕様では「<」「>」は属性値に使ってはいけない。

たまに仕様で許可されてない記号も使えるが、そうしたものが選択できるのはブラウザ依存の偶然でしかない。

対策

こういう文字を使うときは、別の文字に置換でもしないとダメ。例えばDOMロード後に全角の「<」「>」に置換するとか。

参考

Selector problem: $('#\<foo') and $('#bar\>') works, but $('#\<buz\>') doesn't. – jQuery Forum

[JavaScript] 画像の実際のサイズを取得する (jQuery使用)

このエントリーをはてなブックマークに追加
はてなブックマーク - [JavaScript] 画像の実際のサイズを取得する (jQuery使用)
Share on Facebook

  • imgタグのwidth/height要素を指定していない場合、イメージがどう表示されるかはブラウザによって異なる。
  • すべてのブラウザで画像オリジナルのwidth/heightを取得しようとしても、ブラウザ毎にパラメータやメソッドが異なる
  • jQueryを利用すると、下記コードで画像の実際のサイズを取得できる
/*
 * 画像の実際のサイズを取得する
 *
 * @param  string  image  img要素
 */
function getActualDimension(image) {
    var run, mem, w, h, key = "actual";

    // for Firefox, Safari, Google Chrome
    if ("naturalWidth" in image) {
        return {width: image.naturalWidth, height: image.naturalHeight};
    }
    if ("src" in image) { // HTMLImageElement
        if (image[key] && image[key].src === image.src) {return  image[key];}
        
        if (document.uniqueID) { // for IE
            w = $(image).css("width");
            h = $(image).css("height");
        } else { // for Opera and Other
            mem = {w: image.width, h: image.height}; // keep current style
            $(this).removeAttr("width").removeAttr("height").css({width:"",  height:""});    // Remove attributes in case img-element has set width  and height (for webkit browsers)
            w = image.width;
            h = image.height;
            image.width  = mem.w; // restore
            image.height = mem.h;
        }
        return image[key] = {width: w, height: h, src: image.src}; // bond
    }
    
    // HTMLCanvasElement
    return {width: image.width, height: image.height};
}