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はローカル実行するとエラー出ます。
サーバに置けば問題無いのではっていう 多分