2014年9月8日月曜日

ダブルクリック防止

ボタンを押してもなかなかページ遷移してくれない、なんて場合、ユーザはポチポチポチボタンを押してしまうことはよくあること。

そこで二重クリック防止のjavascriptをメモ

html
<button type="button" id="b" onclick = "noclick();">
<font size="1">ダブクリすんな</font>
</button>

javascript
function noclick(){
document.getElementById("b").disabled = true;
setTimeout('ダブクリさせない!', 0);
}

IE7以下では動かんのであしからず


あと、メモ
http://knt45.hatenablog.com/entry/20120821/1345512989

2014年8月25日月曜日

子フレームにダイレクトアクセスされた場合の対処法

子フレームにダイレクトアクセスされた場合の対処法

index.htmlにてiframeで読み込んでいるchild.htmlが、ユーザにダイレクトアクセスされた時に別ページに飛ばす方法をメモ。
他にいい方法あるかな…


child.htmlに下記のjavascriptを埋め込みロード時に実行する。
window.onload = function(){
var url = window.top.location.href.toLowerCase();
var index = "http://hogehoge/index.html"
url=url.substring(0,url.indexOf("?"));

  if(url  != index){
  location.href = "http://hogehoge/error.html";
  }
}

url比較時は、大文字小文字の区別が生じない、パラメータを無視するといったところに配慮。
javascriptが使えないブラウザではどういう対処をしているのか、ようわからない…

2014年8月6日水曜日

子フレームから親フレームを呼び出す

親フレームで読み込んでいるiframeから、親フレームの関数を呼び出します。

親フレーム========================
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>親フレーム</title>

<script language="JavaScript">
function testalert(){
alert('お父さんだよ');
}
</script>

</head>
<body>

<iframe src="./iframe.html" height=300 width=300 name="hey">
</iframe>

<iframe src="./iframe2.html" height=300 width=300 name="heyhey">
</iframe>

</body>
</html>


子フレーム1====================
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>子供1</title>
<script language="JavaScript">
function call(){
alert("兄弟だよ");
window.parent.testalert();
}
</script>
</head>
<body>
<input type="button" value="親を呼びます" onclick = "call()">
</body>
</html>

子フレーム2====================
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>子供2</title>
<script language="JavaScript">
function call(){
  alert("iframe2");
  window.parent.frames['hey'].call();
}
</script>
</head>
<body>
<input type="button" value="兄弟と親を呼びます" onclick = "call()">
</body>
</html>


子フレーム1
window.parent.testalert();

子フレーム2
window.parent.frames['hey'].call();

が重要。


デバッグ時の注意:Chromeはローカル実行するとエラー出ます。
サーバに置けば問題無いのではっていう 多分

2013年6月25日火曜日

R2D

GUIをやるべくruby2.0から使えるR2Dというのを導入することに

https://github.com/takaokouji/r2d


$git clone https://github.com/takaokouji/r2d.git
まずgitで落としてきましょう。

$gem install r2d
これをやるとそれらしい返答がきますが、嘘らしいです。

gitで落としてきたら、そのフォルダに入ります。
$ cd r2d
Version '2.0.0' is to confusing to select ruby interpreter.
なんか怒られますが、そのまま無視で問題なかったです(私は)

$gem build r2d.gemspec
これを打ってbuildします。

  Successfully built RubyGem
  Name: r2d
  Version: 0.0.0
  File: r2d-0.0.0.gem

こんな返答だったら大丈夫のはず。

これでインストール開始!

$ gem install --local r2d-0.0.0.gem
ERROR:  While executing gem ... (Gem::DependencyError)
Unable to resolve dependencies: r2d requires gosu (~> 0.7.47.1)
UserName-no-MacBook-Air:r2d UserName$ port install gosu
Warning: port definitions are more than two weeks old, consider using selfupdate
Error: Insufficient privileges to write to MacPorts install prefix.

gosuがないと怒られているので、gemで入れます。

$ gem install gosuFetching: gosu-0.7.47.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed gosu-0.7.47.1
1 gem installed
Installing ri documentation for gosu-0.7.47.1...
unable to convert "\xCF" from ASCII-8BIT to UTF-8 for lib/gosu.bundle, skipping
Installing RDoc documentation for gosu-0.7.47.1...
unable to convert "\xCF" from ASCII-8BIT to UTF-8 for lib/gosu.bundle, skipping

gosuが入ったらgemでインストール

$ gem install --local r2d-0.0.0.gem
Successfully installed r2d-0.0.0
1 gem installed
Installing ri documentation for r2d-0.0.0...
Installing RDoc documentation for r2d-0.0.0...

これで導入おしまい!!!
exampleの中にサンプルがあるので、動かしてみましょう。





Ruby2.0.0を導入するまでのあれこれ2

前回はrvmにRuby2.0.0-p195を入れることまではできたが、
肝心なuseができなかった。そこのところを調べて修正
まあ、結局友達に聞いたわけですがorz

$ rvm use ruby-2.0.0-p195

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.

前回のエラーです。どうやらターミナルが最初に読み込むbashに問題あったようです

通常Macのターミナルではbashを最初に読み込み、そのタイミングでbashrcを読み込みます。
bashrcには
source "$HOME/.rvm/scripts/rvm"
というのを書く必要があったのですが、まずここでスペルミスをしていたという痛い事故が発生していました。これも大問題の一つだったわけですが、それ以上に問題がありました。bashがbashrcを読み込んでいなかったんです。なんでなのかようわからん。しらん。

そこでbashが読み込むbash_profileに以下の設定を追加しました。

# set rvm PATH
export PATH=$PATH:/Users/UserName/.rvm/bin/  #もともと書かなきゃいけない
source $HOME/.bashrc  #ここが重要

これでbash_profileが読み込まれるついでにbashrcがよみ込まれ、めでたくrvmが動くわけです。長かった・・・・

なんでbashがbashrcを読まなかったのかは、また今度暇な時に調べます。
いろいろ間違ってたら、ごめんなさい。

2013年6月22日土曜日

Ruby2.0.0を導入するまでのあれこれ

Ruby2.0を利用しなければならないけど
rvmの不具合を放置していたため詰んでる途中です
メモメモ

$ rvm install ruby-2.0.0-p195  →→エラー
$ less /Users/arakawahikari/.rvm/log/ruby-2.0.0/fetch.log を確認
$ rvm install ruby-2.0.0-p195 --verify-downloads 1 でうまくいくようだ

$ rvm use ruby-2.0.0-p195

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.

$ /bin/bash --login

$ rvm use ruby-2.0.0-p195

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.


とりあえず https://rvm.io/integration/gnome-terminal/ を確認する

 

2013年6月7日金曜日

CSVをMySQLに読み込ませる

できると便利だね、ということでやってみることに。
だが、カラムが多すぎていろいろとうまくいきませんでした。
インポート自体はできるので、あとは何かな、注意力のなさですかね^0^
mysql5です

> mysql5 --local-infile=1 -uUSERNAME -pPASSWORD TOURISM
まずはこれでログインします。
--local-infile=1 をつけておかないと、幸せになれません。

mysql>create database HOGE;
mysql>use HOGE;
mysql>create table HOGEHOGE(
           ・・・・・
           );
mysql> desc HOGEHOGE;  ちゃんとできてるか確認

データベースと、CSVのデータにあわせたテーブルを作ってやります。
お金を取り扱う場合、intではなくDECIMALがいいそうな。
型についてはあんま調べてません。昔の記憶を頼りに。

CSVをインポート
mysql> LOAD DATA LOCAL INFILE "CSVデータがある絶対パス"  INTO TABLE テーブル名 FIELDS TERMINATED BY ',';

これで挿入完了でございます。
なんか失敗したなって時は、テーブルを空にしましょう
mysql> truncate table 2008basic_ans;

--local-infile=1をつけてログインした理由は、LOAD DATA LOCAL INFILEを使うためです。

おしまい。

2013年6月6日木曜日

MySQLにログインできない時の対処



MySQLのセットアップを毎度忘れて毎度時間をかけてしまう。
前に入れて満足じまいだったので今回はメモする。
無論自分だけでは対処できなかった屑である

久々にSQL使う用事ができたため以下を実行。
>mysql5 -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

こんなエラーが出てきてMySQLに入れない現象に陥った。
パスワードを設定は行っていなかったはず。

いろいろ調べた結果、とりあえずSQLは止めた方がよいと
>sudo /opt/local/share/mysql5/mysql/mysql.server stop

次に、以下のコマンドを実行した
>sudo mysqld_safe5 --user=root --skip-grant-tables
バックグラウンドで動かしたいときは & を語尾にくわえる

>mysql5 -u root
これで一応入れるようになる。mysqld_safe5で実行したのを止めよう。

ROOTユーザを追加したいため、とりあえず
>flush pgesrivile;
を実行する。これをしないとお前じゃ無理と怒られる。

grant all privileges on *.* to root@localhost identified by 'PASSWORD' with grant option;
これで追加できます。

>mysql5 -u root -pPASSWORD

おしまい。
参考になりました。
http://www.goofoo.jp/2011/11/1457

また、上記エラーが出ても
>mysql5
で入ることができる。
でもなんか、やっぱりパスワードあった方がいいよね、いろいろと。

ちなみにmysqld_safe5はCtrl+cで殺せなかった・・・・
>ps aux | grep mysqld_safe
とすると、端末閉じたとしても残っています。
>sudo kill -9 プロセスナンバー
 で殺しましょう。
 なんでなのかな、にわかなのでわかりません。

2013年1月16日水曜日

RubyでCSVをXMLに変換

あまり汎用性はないですが・・・
勉強がてらに書いてみました。
 某ペンを管理するツールを作ろうかと最近思い、とりあえず色の基本データをXMLに書き出すことにしました。
builder/xmlmarkupを使うと随分直感的な表現でXMLが書けます。感動した。
ruby 1.9で書いてます。


# encoding: utf-8
require 'builder/xmlmarkup'
require 'csv'
xm = Builder::XmlMarkup.new :indent => 2
xm.instruct! directive_tag=:xml, :encoding=> 'UTF-8'

print "変換するコピックデータを指定してください:"
readfile = gets.chop.to_s

f=CSV.read("./#{readfile}.csv")     #ファイルオープン
puts xm.root{
  xm.comment! "コピックのRGBデータ"
  xm.copic{
    f.each{|info|
      if /(^+[A-Z]{1,2})/ =~ info[0] or /¥d{1,3}/ =~ info[0] then
        xm.color("type"=>$&){
          xm.name(info[2])
          xm.number(info[0])
          xm.rgbcode(info[1])
        }
      end
    }
  } 
}
print "ファイルネームを指定してください:"
filename = gets.chop.to_s
File.open("#{filename}.xml","w"){|xml|
  xml << xm
}

2012年11月3日土曜日

rvmでRubyをインストールする時の注意

事情あって複数バージョンのRuby環境を導入するはめになった。
rvmを利用します。MacBookAirにはデフォルトで用意されている。多分。

   $rvm list known
現在時点で実行すると以下の様なインストールできるリストが表示されます。
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7-p370
[ruby-]1.8.7[-p371]
[ruby-]1.9.1[-p431]
[ruby-]1.9.2-p180
[ruby-]1.9.2-p290
[ruby-]1.9.2-p318
[ruby-]1.9.2[-p320]
[ruby-]1.9.2-head
[ruby-]1.9.3-preview1
[ruby-]1.9.3-rc1
[ruby-]1.9.3-p0
[ruby-]1.9.3-p125
[ruby-]1.9.3-p194
[ruby-]1.9.3-[p286]
[ruby-]1.9.3-head
ruby-head

# GoRuby
goruby

# TheCodeShop - MRI experimental patches
tcs

# JRuby
jruby-1.2.0
jruby-1.3.1
jruby-1.4.0
jruby-1.6.5
jruby-1.6.5.1
jruby-1.6.6
jruby-1.6.7
jruby-1.6.7.2
jruby-1.6.8
jruby-[1.7.0]
jruby-head

# Rubinius
rbx-1.0.1
rbx-1.1.1
rbx-1.2.3
rbx-1.2.4
rbx[-head]
rbx-2.0.testing

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Kiji
kiji

# MagLev
maglev[-head]
maglev-1.0.0

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby -- Not implemented yet.
ironruby-0.9.3
ironruby-1.0-rc2
ironruby-head
Ruby1.8.6と1.8.7が必要だったので上記のリストを見ながら導入します。 []は省くことができるようです。
   $rvm install 1.8.6
No binary rubies available for: osx/10.7/x86_64/ruby-1.8.6-p420.
Continuing with compilation. Please read 'rvm mount' to get more information on binary rubies.
The provided compiler '/usr/bin/gcc' is LLVM based, it is not yet fully supported by ruby and gems, please read `rvm requirements`.
怒られた…gccが気に喰わぬといった雰囲気です。いじりましたが以下の方法で解決できました。
$rvm install 1.8.6 --with-gcc=clang