星期六, 2月 22, 2014

[jQuery Plugins] 影片在bootstrap carousel中時,當影片播放時如何停止carousel

很多網站的banner都會使用bootstrap carousel這個外掛
當使用者點擊carousel的影片播放時,需要暫停carousel自動換頁的動作。

目前影片是採用Vimeo,所以你可以使用Froogaloop這個元件來取得Vimeo的控制。
完整範例可參考:http://developer.vimeo.com/player/js-api

一開始在用的時候,沒有認真看文件的說明,導致AddEvent的事件完全無動作,大家要注意嵌入的影片格式要包含api=1與player_id=


相關範例如下:

        var $myCarousel = $("#carousel-iangel");
        $myCarousel.on("slide.bs.carousel", function(event) {
            $.console("change slide");
            var $currentSlide = $myCarousel.find(".active iframe");
            // exit if there"s no iframe, i.e. if this is just an image and not a video player
            if (!$currentSlide.length) { return; }
            
            // pass that iframe into Froogaloop, and call api("pause") on it.
            var player = Froogaloop($currentSlide[0]);
            player.api("pause");
        });

        
        var iframe = $("#videoIntro")[0],
            player = $f(iframe); //$f 這是Froogaloop提供的方法

        // When the player is ready, add listeners for pause, finish, and playProgress
        player.addEvent("ready", function() {
            player.addEvent("play", function(id) {
                // $.console("id:" + id + " play");
                $myCarousel.carousel("pause");
            });
            player.addEvent("pause", function(id) {
                // $.console("id:" + id + " pause");
                $myCarousel.carousel("pause");
            });
            player.addEvent("finish", function(id) {
                // $.console("id:" + id + " finish");
                $myCarousel.carousel("cycle");
            });
            player.addEvent("loadProgress", function(data, id) {
                // $.console(data.seconds + "s played");
            });
        });

星期一, 2月 17, 2014

[VIM] 如何做全選+複製

常常會用的VIM指令
Reference:
http://stackoverflow.com/questions/1620018/vi-editor-copy-all-the-lines-to-clipboard

You should yank the text to the * or + registers:
gg"*yG
Explanation:
  • gg
    • gets the cursor to the first character of the file
  • "*y
    • Starts a yank command to the register * from the first line, until...
  • G
    • go the end of the file

[Shell] 利用 Shell Script 製作選單

如果想要用shellscript寫出程式執行選單可參考以下範例。 拿來寫一些每天常常會打的指令蠻方便的:D
#!/bin/bash
# Bash Menu Script Example

PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "you chose choice 1"
            ;;
        "Option 2")
            echo "you chose choice 2"
            ;;
        "Option 3")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

星期四, 1月 23, 2014

[Chrome APP] POSTMAN - multipart/form-data 上傳檔案

postman是一個好用的reset client app,
如果你要透過他來測試相關上傳api時,
記得content-type要設multipart/form-data(久久用一次都會漏掉,筆記)。
不然server會回傳415的http status


星期一, 1月 13, 2014

[HTML] 加了上標與下標字體

有產品的LOGO需要顯示plus的符號,也可以應用在數字公式上
順便筆記一下

上標文字或符號,英文是 superscript => HTML 標籤
下標文字或符號,英文是 subscript => HTML 標籤 <sub>

範例:

$("body").append("A+");

星期二, 12月 17, 2013

[JSP] IE8 無法下載檔案


最近客戶遇到在IE8不能下載檔案的問題,需要加了一些cache的header即可解決。
請參考微軟的說明:http://support.microsoft.com/kb/316431 範例如下:



String ua = request.getHeader("User-Agent");
         if(ua != null){
          //System.out.println("user-agent:" + ua);
          if(ua.indexOf("MSIE 8.0") > 0){
           response.setHeader("Cache-Control","private");
                    response.setHeader("Pragma","private");
          }
         }

星期一, 12月 02, 2013

[Java] 如何將上傳streaming透過Http Client再上傳到另外一個Server

目前有一個需求是要將上傳api拿到的Stream再透過REST API上傳至另一個儲存空間。
採用的架構是透過Java Jersey + Apache HttpClient 4.3

測試範例如下:

[Java] 下載檔案的中文編碼錯誤如何解決

當透過瀏覽器下載檔案時,需要處理不同瀏覽器對檔案名稱該如何正確編碼的問題。
可以參考[2]的實驗結果。

目前針對IE採用UrlEncode,chrome與firefox採用Mimetype base64編碼,其他未偵到的也都採用base64 :D,base64編碼可直接使用 MimeUtility.encodeText(fileName, "UTF-8", "B");即可。

範例碼如下:
String userBrowser = request.getHeader(HEADER_USER_AGENT);
       
       if(userBrowser != null){
        
        log.debug(String.format("User agent: %s", userBrowser));
        if(userBrowser.toLowerCase().indexOf("mozilla") > -1
          && userBrowser.toLowerCase().indexOf("msie") < 0 
          && userBrowser.toLowerCase().indexOf("chrome") < 0){
         
         log.debug("Detect 'FireFox' browser");
//         Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0

         fileName = MimeUtility.encodeText(fileName, "UTF-8", "B");
         
        }else if(userBrowser.toLowerCase().indexOf("msie") > -1){
        
         log.debug("Detect 'IE' browser");
//         Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; MATP)
         //cosa url encoder

         fileName = URLEncoder.encode(fileName);  
         
        }else{
        
//         Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
         log.debug("Common attachment");
         
         fileName = MimeUtility.encodeText(fileName, "UTF-8", "B");
        }
       }
       else{
        
        log.debug("Cannot find User-Agent header");
        
//        encoding - the encoding to be used. Currently supported values are "B" and "Q".
//        If this parameter is null, then the "Q" encoding is used if most of characters to be encoded are in the ASCII charset, otherwise "B" encoding is used.
        fileName = MimeUtility.encodeText(fileName, "UTF-8", "B");

       }
       
       log.debug("Filename encoded:" + fileName);
       String contentDisposition = String.format("%s ;filename=\"%s\"", valOfDisposition, fileName);

       log.debug("Content diposition:" + contentDisposition);
       builder.header(HEADER_CONTENT_DISPOSITION,contentDisposition);


測試檔案:
ライセンス期限切中文中文kumokura_010507.exe

Base64編碼:

=?UTF-8?B?44Op44Kk44K744Oz44K55pyf6ZmQ5YiH5Lit5paH5Lit5paHa3Vt?= =?UTF-8?B?b2t1cmFfMDEwNTA3LmV4ZQ==?=



參考:
[1] 錯誤訊息:「Internet Explorer cannot download a file (Internet Explorer 無法下載檔案)」

[2] Unicode网页中上传下载文件时发生文件名乱码的问题

星期六, 11月 30, 2013

[jQuery plugin] jQuery validatation 文件

寫網站如果使用jquery的話,一定對jquery validation這個外掛很熟。 今天寫一些功能的時候,找到了一個介紹如何使用的網站:D 有興趣的人可以參考: http://jqueryvalidation.org/

星期三, 11月 27, 2013

[jQuery API] 如何使用$ajax進行 multiple-part/form-data 傳輸

這幾天整合summernote編輯器的上傳,
順便記錄一下如何使用jquery $ajax進行multiple-part的上傳方式
var formdata = new FormData();
//指定欄位的name與binary string
    formdata.append("fileToUpload", file);
    $.ajax({
        data: formdata,
        type: "POST",
        url: "your_api/upload_imgur/" + uid,
        // cache: false,
        contentType: false,
        // dataType: "json",//用這個叫api會404
        processData: false,
        success: function(rJSON) {
            $.console("send file callback:");
}
});
參考https://github.com/HackerWins/summernote/issues/72

星期二, 10月 01, 2013

[jQuery] beforeunload 踩到雷

由於在處理某個議題了,加入監聽beforeunload事件,目前由於有透過window.location.href進行檔案下載的動作,好死不死在chrome,也會因為這個指令而跑到beforeunload的事件。
google找到了對岸的實驗結果(http://www.w3help.org/zh-cn/causes/BX2047),如下表,不過應該在這瀏覽器會更新的時代只能僅供參考,要自已實驗XD
执行结果汇总入表:
IEFirefoxChrome SafariOpera
关闭当前浏览器窗口事件被触发事件被触发事件被触发不支持该事件
导航到另一个进入一个新的地址或选择一个喜欢的位置事件被触发事件被触发事件被触发不支持该事件
单击后退,前进,刷新,或主页按钮事件被触发事件被触发事件被触发不支持该事件
点击一个链接到新页面事件被触发事件被触发事件被触发不支持该事件
调用 anchor.click方法事件被触发不支持此方法1不支持此方法1不支持该事件
调用 document.write方法事件被触发事件被触发事件未触发不支持该事件
调用 document.open方法事件被触发事件被触发事件未触发不支持该事件
调用 document.close方法事件未触发事件未触发事件未触发不支持该事件
调用 window.open方法,窗口名称设置值为 _self事件被触发事件被触发事件被触发不支持该事件
调用 window.navigate事件被触发不支持此方法2不支持此方法2不支持该事件
调用 NavigateAndFind方法事件被触发不支持此方法3不支持此方法3不支持此方法3
调用 location.replace 方法事件被触发事件被触发事件被触发不支持该事件
调用 location.reload 方法事件被触发事件被触发事件被触发不支持该事件
指定一个 location.href 属性的新值事件被触发事件被触发事件被触发不支持该事件
使用 submit 按键提交表单事件被触发事件被触发事件被触发不支持该事件
调用 form.submit 方法事件被触发事件被触发事件被触发不支持该事件
调用 javascipt: 伪协议事件被触发事件未触发事件未触发不支持该事件
调用 mailto: 伪协议事件未触发事件未触发事件被触发不支持该事件
调用自定义伪协议事件被触发事件被触发事件被触发不支持该事件

星期四, 9月 26, 2013

[Shell] 監控JVM使用狀態

為了測試目前記憶體佔用的狀態所寫的shell。


cosa-memory.sh


#!/bin/sh

#author: ken tsai
#date: 2013/7/11

TOMCAT_PID=$(ps aux | grep /bin/java |grep -v grep | grep 'logging' |awk '{print $2}') 

echo "tomcat pid :$TOMCAT_PID"
echo "######################memory monitor##################"
jstat -gcutil -h 10 $TOMCAT_PID 1000

-gcutil Option


Summary of Garbage Collection Statistics
列名描述
S0survivor 0区利用率。
Survivor space 0 utilization as a percentage of
the space’s current capacity.
S1survivor 1区利用率。
Survivor space 1 utilization as a percentage of
the space’s current capacity.
Eeden区利用率。
Eden space utilization as a percentage of
the space’s current capacity.
O年老代空间利用率。
Old space utilization as a percentage of
the space’s current capacity.
P永生代空间利用率。Permanent space utilization as a percentage of
the space’s current capacity.
YGCyoung gc次数。
Number of young generation GC events.
YGCTyoung gc耗时。
Young generation garbage collection time.
FGCfull gc次数。
Number of full GC events.
FGCTfull gc耗时。
Full garbage collection time.
GCTGC总耗时。
Total garbage collection time.
參考資料
http://itzoo.info/?p=256

星期二, 9月 24, 2013

[Mongodb] child process failed, exited with error number 100 無法啟動monogo

在做硬碟塞爆測試的時候,為了將新的build丟上去機器後重開機,
結果會死在monogo無法啟動。

錯誤訊息如下

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
about to fork child process, waiting until server is ready for connections.
forked process: 11871
all output going to: //storage/COSA//Mongo//log/mongodb.log
ERROR: child process failed, exited with error number 100

追了一下Monog噴的log


死在Insufficient free space for journal files 的錯誤,不過也是噴child process failed, exited with error number 100,這個錯誤的產生說是不正常的mongo db關機所造成。

星期一, 9月 23, 2013

[ShellScript] 用dd指令做硬碟塞爆測試

最近需要透過塞爆硬碟測試一些API,找到了網路上dd指令的介紹。
順便寫了一個簡單的shell script做測試。

of:檔案要存放的位置

//做1G
dd if=/dev/zero of=/tmp/1gb count=1024 bs=1M

//做10G
dd if=/dev/zero of=/tmp/10gb count=10240 bs=1M

//做100G
dd if=/dev/zero of=/tmp/100gb count=102400 bs=1M


//做100G
dd if=/dev/zero of=/tmp/100gb count=100 bs=1G


包裝成shell script使用

目前先都將產生的檔案產生在tmp的目錄下

#!/bin/bash

dummy_path="/tmp/"
count=$1
block_size=$2
block_count=$3

if [ -z "$count" ] || [ -z "$block_size"] || [ -z "$block_count"]
then
echo "Please input paramters for creating dummy file"
else

for ((index=0;index<$count;index++))
do
#echo "file index: $index"
now=$(date +"%m-%d-%Y-%H-%M-%S")
filename="dummyfile_no_${index}_${now}"
echo "create dummy file($index):$filename"
dd if=/dev/zero of="$dummy_path/$filename" count=$block_count bs=$block_size
done
fi

exit 0

$>./dummy_creator 10 1M 1024 # 產生十個1G的檔案

星期三, 9月 18, 2013

[jQuery] 如何控制textbox按enter後往下一個textbox移動

在提供使用者使用表單填寫資料時,如果欄位爆多的時候,
通常使用者會習慣打完資料按enter鍵後能自動往下一個欄位移動。
如果你有這個需求可以參考以下這個範例。
http://jquerybyexample.blogspot.com/2011/05/how-to-set-focus-on-next-textbox-on.html

實作方法透過偵測現在有多少input textbox,再透過遞增textbox index方式即可簡單做到
$('input:text')[nextIndex].focus();

把它改成外掛會更好使用。


星期三, 9月 11, 2013

[AngularJS] 如何重設表單

最近透過Angular在送出表單時,想要清除表單資料,於是將綁定欄位的model重設時,
會造成表單欄位有設require屬性,會丟出相對應的錯誤訊息。
因為在1.0.x版本時,這樣並沒有將整個表單的狀態重設

可以參考這篇
http://stackoverflow.com/questions/12603914/reset-form-to-pristine-state-angularjs-1-0-x

不過在1.1.x版提供一行簡單的指令可以解決重設表單的問題

 $scope.你的表單id.$setPristine();

範例
                             
 $scope.signup_form.$setPristine();
                             

其他你感興趣的文章

Related Posts with Thumbnails