FreeBSD 上安裝 htop 的問題

問題1:
htop(1) requires linprocfs(5) to be mounted. If you don’t
have it mounted already, please add this line to /etc/fstab
and run `mount linproc`:
linproc /compat/linux/proc linprocfs rw 0 0

問題2:
mount: /usr/compat/linux: No such file or directory

解決:

  1. mkdir -p /usr/compat/linux/proc
  2. echo "linproc /compat/linux/proc linprocfs rw 0 0" >> /etc/fstab
  3. mount linproc
  4.  
  5. cd /usr/ports/sysutils/htop
  6. make install clean

ASP Multi Page 通用分頁函數

  1.  ' @numbers : 總記錄數目
  2.  ' @per_page: 每頁顯示記錄
  3.  ' @current_page: 目前分頁
  4.  ' @url_query_string: 目前頁面的Query String
  5.  Function get_multi_page(numbers,per_page,current_page,url_query_string)
  6.      current_page = Int(current_page)
  7.  
  8.      Dim url
  9.      url = Request.ServerVariables("Script_Name") & url_query_string
  10.  
  11.      get_multi_page=""
  12.  
  13.      Dim page, offset, page_counter
  14.  
  15.      If Int(numbers) > Int(per_page) Then
  16.          page = 10
  17.          offset = 2
  18.  
  19.          Dim pages,from_page,to_page
  20.  
  21.          If numbers Mod Cint(per_page) = 0 Then
  22.              pages = Int(numbers / per_page)
  23.          Else
  24.              pages = Int(numbers / per_page) + 1
  25.          End If
  26.  
  27.          from_page = current_page - offset
  28.          to_page = current_page + page - offset - 1
  29.  
  30.          If page > pages Then
  31.              from_page = 1
  32.              to_page = pages
  33.          Else
  34.  
  35.          If from_page < 1 Then
  36.              to_page = current_page + 1 - from_page
  37.              from_page = 1
  38.  
  39.              If (to_page - from_page) < page And (to_page - from_page) < pages Then to_page = page
  40.              ElseIF to_page > pages Then
  41.                  from_page = current_page - pages + to_page
  42.                  to_page = pages
  43.  
  44.                  If (to_page - from_page) < page And (to_page - from_page) < pages Then from_page = pages - page + 1
  45.              End If
  46.          End If
  47.  
  48.          get_multi_page = "<a href='" & url & "page=1'><<</a> "
  49.  
  50.          For page_counter = from_page TO to_page
  51.              If page_counter <> current_page Then
  52.                  get_multi_page = get_multi_page & "<a href='" & url & "page=" & page_counter & "'>[" & page_counter & "]</a> "
  53.              Else
  54.                  get_multi_page = get_multi_page & "<b>[" & page_counter & "]</b> "
  55.              End If
  56.          Next
  57.  
  58.          If Int(pages) > Int(page) Then
  59.              get_multi_page = get_multi_page & " ... <a href='" & url & "page=" & pages & "'> [" & pages & "] >></a>"
  60.          Else
  61.              get_multi_page = get_multi_page & "<a href='" & url & "page=" & pages & "'>>></a>"
  62.          End If
  63.      End If
  64.  End Function

Apache 實現用戶驗證 Authentication

  1. # 指定需要使用驗證登入的目錄
  2. <Directory "/home/test">
  3. Options Indexes MultiViews
  4. AllowOverride AuthConfig # 需要進行驗證
  5. Order allow,deny
  6. Allow from all
  7. </Directory>
  8.  
  9. # 在指定的目錄中生成 .htaccess
  10. vim /home/test/.htaccess
  11.  
  12. AuthName "My Share File"
  13. AuthType Basic
  14. AuthUserFile /home/test/.htpasswd
  15. require valid-user
  16.  
  17. # 在相對的目錄中生成相應的用戶的 .htpasswd
  18. htpasswd -c /home/test/.htpasswd [username]
  19.  
  20. # 當要修改密碼時只需要將 -c 改為 -m
  21. htpasswd -m /home/test/.htpasswd [username]
  22.  
  23. # 最後重新執行 Apache

參考: http://www.5ilinux.com/apache03.html

FreeBSD install Git and GitWeb

  1. # 先從 devel/git 的 ports 中安裝 git
  2. # 同時間選取 gitweb 以提供網頁界面
  3. # 之後遇到的設定頁照原來的 OK OK 就可以
  4. cd /usr/ports/devel/git
  5. make install clean
  6.  
  7. # 增加 git 的使用者,並指定 git 的家用目錄為 /home/git
  8. # 同時間使用 git-shell 作為 shell 指令, uid, gid 為 9418
  9. pw groupadd -n git -g 9418
  10. pw useradd -n git -u 9418 -g git -c git -d /home/git -s /usr/local/libexec/git-core/git-shell -h -
  11.  
  12. # 新增 repo (倉庫 work),並更改屬性
  13. chown git:git /home/git
  14. chmod 755 /home/git
  15.  
  16. mkdir /home/git/work/
  17. chmod 775 /home/git/work/
  18. chown git:git /home/git/work/
  19.  
  20. # 設定除了 commit 外還可以使用額外指令的用戶
  21. # 如果使用者只需要 commit,就可以不加進來
  22. vi /etc/group
  23. git:*:9418:zeuxis,neo
  24.  
  25. # 設定 SSH 認證金鑰,因為 git 要用到這東西
  26. # 每個使用者的 SSH Key 也要加進 authorized_keys.
  27. # 每個 key 為一行
  28. mkdir /home/git/.ssh/
  29. chmod 700 /home/git/.ssh/
  30.  
  31. touch /home/git/.ssh/authorized_keys
  32. chmod 600 /home/git/.ssh/authorized_keys
  33. chown -R git:git /home/git/.ssh/
  34.  
  35. # 生成指定用戶的 SSH Key
  36. # 在安裝情況下是使用 ssh-keygen -d 生成 dsa
  37. # 另還可用 ssh-keygen -t rsa 生成 rsa
  38. # 生成時 Enter passphrase 無視按下 enter 即可
  39. su username
  40. ssh-keygen -d
  41. su root
  42. cat /home/username/.ssh/id_dsa.pub > /home/git/.ssh/anthorized_keys
  43.  
  44. # 接著設定 git 的 repo
  45. mkdir /home/git/work/test.git
  46. cd /home/git/work/test.git && git init --bare --shared
  47.  
  48. # 接著進行一個簡單的測試
  49. mkdir /tmp/test && cd /tmp/test && git init
  50. echo "This is a test" > index.html
  51. git add
  52. git commit -m 'added index.html'
  53.  
  54. # 之後 commit 到遠端的 repo
  55. git remote add origin username@example.com:work/test.git
  56. git push origin master
  57.  
  58. # 之後再設定 gitweb
  59. mkdir /home/git/public_html
  60. chmod 755 /home/git/public_html
  61. chown git:git /home/git/public_html
  62. cp /usr/local/share/examples/git/gitweb/git* /home/git/public_html
  63.  
  64. # 再設定 apache
  65. <VirtualHost *:80>
  66.   ServerAdmin no-reply@example.com
  67.   DocumentRoot "/home/git/public_html"
  68.   ServerName git.example.com
  69.  
  70.   <Directory "/home/git/public_html">
  71.     Options ExecCGI
  72.     Order allow,deny
  73.     Allow from all
  74.  
  75.     DirectoryIndex gitweb.cgi
  76.     AddHandler cgi-script .cgi
  77.   </Directory>
  78. </VirtualHost>
  79.  
  80. # 之後再編輯 gitweb.cgi
  81. our $projectroot = "/home/git/work";
  82. our $home_link_str = "work";
  83. our $site_name = "git.example.com"
  84. our $projects_list_description_width = 40; # 調整 description 空間
  85.  
  86. # 修改 test.git 的描述
  87. echo "test.git" > /home/git/work/test.git/description
  88.  
  89. # 修改 test.git 的 owner
  90. vim /home/git/work/test.git/config
  91.  
  92. # 在其後面加入
  93. [gitweb]
  94. owner = zeuxis
  95. url = git://git.example.com/work/test.git
  96. url = username@git.example.com:work/test.git
  97.  
  98. # 最後就是設定 Git protocol
  99. # 在 /etc/rc.conf 最後加入這段
  100. git_daemon_enable="YES"
  101. git_daemon_directory="/home/git"
  102. git_daemon_flags="--syslog --base-path=/git --export-all"
  103.  
  104. # 之後啟動 Git Daemon
  105. /usr/local/etc/rc.d/git_daemon start
  106.  
  107. # 如果以上面這句子會出現停在 starting 的字眼可以改為以下句式
  108. # 送出後接兩下 Enter 結束即可
  109. /usr/local/etc/rc.d/git_daemon start &
  110.  
  111. # 檢查是否在執行
  112. ps -eaf | grep -v grep | grep git
  113.  
  114. # 如有東西就可以再測試可否使用了
  115. cd /tmp/test
  116. rm -Rf *
  117. git clone git://git.example.com/work/test.git
  1. 參考:
  2. http://dennylin93.wordpress.com/2010/02/04/how-to-setup-git/
  3. http://vanix.blogspot.com/2010/01/setup-git-server-on-freebsd.html
  4. http://plog.longwin.com.tw/my_note-unix/2009/05/08/build-git-env-over-ssh-2009
  5. http://plog.longwin.com.tw/my_note/2005/12/28/ssh_keygen_no_passwd
  6. http://hi.baidu.com/d_life/blog/item/c52d75013566a5dd267fb54f.html

Ubuntu Putty/Pietty 中亂碼的解決

  1. 1. 先通過 ssh 登入
  2. 2. 在終端機中輸入 LANG=zh_HK.UTF-8
  3. 3. 再到 Pietty 中 "選項(Option) > 字元編碼(Encoding) > Unicode UTF-8"
  4. 4. 再用 ls 看看是原來亂碼的目錄是否亂碼就可以了
  5.  
  6. 另外如果不想在每次登入後都重覆 2-4 步驟的.可以這樣子
  7. echo "LANG=zh_HK.UTF-8" >> ~/.vimrc
  8.  
  9. 目前的語系可以經這方法查看
  10. cat /var/lib/locales/supported.d/local
  11.  
  12. 1. 至於增加 BIG5 語系可以編輯 /var/lib/locales/supported.d/zh-hant
  13. 2. 在尾增加 zh_HK.Big5 Big5
  14. 3. sudo locale-gen

FreeBSD 中 SSH 的 su 不能登入和設置

  1. #SSH 進入 FreeBSD 後切換到用戶 (su username) 時發生了
  2. This account is currently not available.
  3.  
  4. # 解決方法是使用
  5. chpass [username]
  6.  
  7. # 之後直接修改 shell 的那一行,指向到需要的 shell language
  8. # 如:
  9. Shell: /bin/csh
  10.  
  11. # 之後存檔,再 su 就應該可以了
  12.  
  13. #####
  14. 注:
  15. 別直接修改 /etc/passwd 和 /etc/master.passwd
  16. 否則會出現
  17.  
  18. chpass: entry inconsistent
  19. chpass: pw_copy: Invalid argument
  20.  
  21. #####
  22. 另外要將 /usr/sbin/nologin 當為可用 shell
  23. 可以直接將其加入到 /etc/shells
  24. 此舉可以配和 proftpd.conf 作為只可以登入 ftp 而不可以登陸 ssh
  25.  
  26. #####
  27. 另外要指定可使用 ssh 中誰不可以進入,改 Port 可以參考這篇
  28. http://linux.vbird.org/linux_server/0310telnetssh.php#ssh_sshdconfig

FreeBSD 換成 UTF-8 中文環境 & VIM 安裝

  1. # 首先切換到 root
  2. cd /root
  3.  
  4. # 查看 root 自身用的是什 shell language
  5. cat /etc/passwd
  6.  
  7. # 因為這裡我用的是 csh,所以就用 ee 指令來編輯
  8. ee .cshrc
  9.  
  10. # 在 .cshrc 最下面加入
  11. setenv LC_ALL zh_TW.UTF-8
  12. setenv LANG zh_TW.UTF-8
  13. setenv CLICOLOR
  14. set color
  15.  
  16. # 最後 putty 設定
  17. Windows > Translation > charset > 設為 UTF-8
  18. Windows > Appreance > FontSettings > 設定為支援 BIG5 的字體 (應該用預設的就可以了)
  19.  
  20. # 如果用 pietty 的只需要
  21. 選項 > 字元編碼 >Unicode UTF-8
  22.  
  23. # 最後再來安裝 vim 編輯器
  24. cd /usr/ports/editors/vim
  25. make install && make install clean
  26.  
  27. ### 不過在安裝 vim 時遇到了一些依賴 library 版本太低需要升級
  28.  
  29. # 所需切換到 xextproto,接著解除安裝,再重新安裝
  30. cd /usr/ports/x11/xextproto
  31. make deinstall
  32. make install
  33.  
  34. # 再切回 vim port 安裝,發現 inputproto 版本也過低,步驟同上來升級
  35. cd /usr/ports/x11/inputproto
  36. make deinstall
  37. make install
  38.  
  39. # 又再回到 vim port 安裝,發現 X11 版本太低了,步驟同上再升級
  40. cd /usr/ports/x11/libX11
  41. make install
  42.  
  43. # 在安裝 libX11 時他的依賴版本 xorg-macro 也太低,同步
  44. cd /usr/ports/devel/xorg-macros
  45. make deinstall
  46. make install
  47.  
  48. # 最後回到 libX11 再次
  49. cd /usr/ports/x11/libX11
  50. make deinstall
  51. make install
  52.  
  53. # 最後就是安裝 vim 就應該成功了

記錄一下基本的 .vimrc
如果放在 /usr/home/vimrc 下則為全部用戶共用
如果要獨立用戶則只需要放在 /usr/home/[user]/.vimrc 則可

DownLoad: .vimrc
  1. set ruler
  2. set number
  3. set hlsearch
  4. set incsearch
  5. set nocompatible
  6. set backspace=start,indent,eol
  7. set cin
  8. set tabstop=4
  9. set shiftwidth=4
  10. set smartindent
  11. set autoindent
  12. set showmatch
  13. set cursorline
  14. set fileencodings=utf-8,ucs-bom
  15. set termencoding=utf-8,ucs-bom
  16. set encoding=utf-8
  17. set history=400
  18. set ignorecase
  19. set nobackup
  20. syntax on
  21.  
  22. behave mswin

參考: http://blog.tshes.tcc.edu.tw/post/1/681

Python web.py 的安裝 (mod_wsgi)

安裝 easy_install

  1. 1. 下載 http://peak.telecommunity.com/dist/ez_setup.py
  2. 2. 命令提示字元檔中輸入 python ez_setup.py
  3. 3. 如果一直停留在下載劃面
  4.    - 到 http://pypi.python.org/pypi/setuptools
  5.    - 下載相應的 setuptools-X.XXXX-pyX.X.egg
  6.      (如: setuptools-0.6c11-py2.6.egg)
  7.    - 放於同 ez_setup.py 的目錄中
  8. 4. 環境變量的 PATH 中加入
  9.    - X:\Application\Python;X:\Application\Python\Scripts;

安裝 web.py

  1. easy_install web.py

測試用的 main.py

  1.  import web
  2.  
  3.  urls = (
  4.      '/(.*)', 'hello'
  5.  )
  6.  app = web.application(urls, globals(), autoreload=False)
  7.  application = app.wsgifunc()
  8.  
  9.  class hello:       
  10.      def GET(self, name):
  11.          if not name:
  12.              name = 'World'
  13.          return 'Hello, ' + name + '!'
  14.  
  15.  if __name__ == "__main__":
  16.      application.run()

位於同目錄的 .htaccess

  1. <IfModule mod_rewrite.c>     
  2.   RewriteEngine on
  3.   RewriteBase /webpy
  4.   RewriteCond %{REQUEST_URI} !^/icons
  5.   RewriteCond %{REQUEST_URI} !^/favicon.ico$
  6.   RewriteCond %{REQUEST_URI} !^(/.*)+main.py/
  7.   RewriteRule ^(.*)$ main.py/$1 [PT]
  8. </IfModule>
  9.  
  10. <Files main.py>
  11.     SetHandler wsgi-script
  12.     Options ExecCGI FollowSymLinks
  13. </Files>

最後打開網址: http://localhost/webpy/main.py

WordPress 插件架構

整個插件結構是以 WordPress 為基礎分離再整合出來的..記錄一下
不過應該有安全性的問題,因為 Plugin 路徑使用的不是絕對路徑
不過這種技術其實有些不好..因為是 Hook …

一共三個檔案:
- index.php
- plugin.php
- plugin/get_content.php

DownLoad: plugin.php
  1.  <?php
  2.  class Plugin {
  3.      private static $plugin_folder = "plugin";
  4.      private static $plugin_filter = array();
  5.      private static $current_filter= array();
  6.      private static $plugin_action = array();
  7.      private static $merged_filter = array();
  8.     
  9.      public static function init($settings = array()) {
  10.          if (is_array($settings) === true && empty($settings) === false) {
  11.              foreach($settings as $k => $v) {
  12.                  self::$$k = $v;
  13.              }
  14.          }
  15.  
  16.          foreach(glob(self::$plugin_folder."/*") as $file_path) {
  17.              require_once $file_path;
  18.          }
  19.      }
  20.     
  21.      public static function add_filter($tag, $function_name, $priority = 10, $total_arguments = 1) {
  22.          $unique_id = self::get_unique_id($tag, $function_name, $priority);
  23.          self::$plugin_filter[$tag][$priority][$unique_id] = array(
  24.              'function' => $function_name,
  25.              'total_arguments' => $total_arguments
  26.          );
  27.          unset(self::$merged_filter[$tag]);
  28.      }
  29.     
  30.      public static function apply_filter($tag, $value) {
  31.          $arguments = array();
  32.          self::$current_filter[] = $tag;
  33.         
  34.          if (isset(self::$plugin_filter[$tag]) === false) {
  35.              array_pop(self::$current_filter);
  36.              return $value;
  37.          }
  38.         
  39.          if (isset(self::$merged_filter[$tag]) === false) {
  40.              ksort(self::$plugin_filter[$tag]);    // 優先櫂, 10 是最晚執行, 1 是最先執行
  41.              $merged_filter[$tag] = true;
  42.          }
  43.         
  44.          reset(self::$plugin_filter[$tag]);
  45.         
  46.          if (empty($arguments)) {
  47.              $arguments = func_get_args();
  48.          }
  49.  
  50.          foreach(self::$plugin_filter[$tag] as $priority) {
  51.              foreach($priority as $filter) {
  52.                  if (is_null($filter['function']) === false) {
  53.                      $arguments[1] = $value;
  54.                      $value = call_user_func_array($filter['function'], array_slice($arguments, 1, (int) $filter['total_arguments']));
  55.                  }
  56.              }
  57.          }
  58.         
  59.          array_pop(self::$current_filter);
  60.         
  61.          return $value;
  62.      }
  63.     
  64.      public static function remove_filter($tag, $function_name, $priority = 10) {
  65.          $unique_id = self::get_unique_id($tag, $function_name, $priority);
  66.         
  67.          if (isset(self::$plugin_filter[$tag][$priority][$function_name]) === true) {
  68.              unset(self::$plugin_filter[$tag][$priority][$function_name]);
  69.             
  70.              if (empty(self::$plugin_filter[$tag][$priority])) {
  71.                  unset(self::$plugin_filter[$tag][$priority]);
  72.              }
  73.             
  74.              unset(self::$merged_filter[$tag]);
  75.          }
  76.      }
  77.     
  78.      public static function remove_all_filter($tag, $priority = false) {
  79.          if (isset(self::$plugin_filter[$tag]) === true) {
  80.              if ($priority !== false && isset(self::$plugin_filter[$tag][$priority])) {
  81.                  unset(self::$plugin_filter[$tag][$priority]);
  82.              }else{
  83.                  unset(self::$plugin_filter[$tag]);
  84.              }
  85.          }
  86.         
  87.          if (isset(self::$merged_filter[$tag]) === true) {
  88.              unset(self::$merged_filter[$tag]);
  89.          }
  90.      }
  91.     
  92.      public static function add_action($tag, $function_name, $priority = 10, $total_arguments = 1) {
  93.          return self::add_filter($tag, $function_name, $priority, $total_arguments);
  94.      }
  95.     
  96.      public static function do_action($tag, $argument = '') {
  97.          if (isset(self::$plugin_action) === false) {
  98.              self::$plugin_action = array();
  99.          }
  100.         
  101.          if (isset(self::$plugin_action[$tag]) === false) {
  102.              self::$plugin_action[$tag] = 1;
  103.          }else{
  104.              ++self::$plugin_action[$tag];
  105.          }
  106.         
  107.          self::$current_filter[] = $tag;
  108.         
  109.          if (isset(self::$plugin_filter[$tag]) === false) {
  110.              array_pop(self::$current_filter);
  111.              return;
  112.          }
  113.         
  114.          $arguments = array();
  115.          if (is_array($argument) === true && count($argument) === 1 && isset($argument[0]) && is_object($argument[0])) {
  116.              $arguments[] = &$argument[0];
  117.          }else{
  118.              $arguments[] = $argument;
  119.          }
  120.         
  121.          for($i=2; $i<func_num_args(); $i++) {
  122.              $arguments[] = func_get_arg($i);
  123.          }
  124.  
  125.          if (isset(self::$merged_filter[$tag]) === false) {
  126.              ksort(self::$plugin_filter);
  127.              self::$merged_filter[$tag] = true;
  128.          }
  129.         
  130.          reset(self::$plugin_filter[$tag]);
  131.         
  132.          foreach(self::$plugin_filter[$tag] as $priority) {
  133.              foreach($priority as $filter) {
  134.                  if (is_null($filter['function']) === false) {
  135.                      call_user_func_array($filter['function'], array_slice($arguments, 0, (int) $filter['total_arguments']));
  136.                  }
  137.              }
  138.          }
  139.         
  140.          array_pop(self::$current_filter);
  141.      }
  142.     
  143.      public static function remove_action($tag, $function_name, $priority = 10, $total_arguments = 1) {
  144.          return self::remove_filter($tag, $function_name, $priority, $total_arguments);
  145.      }
  146.     
  147.      public static function remove_all_actions($tag, $priority = false) {
  148.          return self::remove_all_filter($tag, $priority);
  149.      }
  150.     
  151.      private static function get_unique_id($tag, $object, $priority) {
  152.          static $filter_id_counter = 0;
  153.         
  154.          if (is_string($object)) {
  155.              return $object;
  156.          }
  157.         
  158.          if (is_object($object)) {
  159.              $object = array($object, '');
  160.          }else{
  161.              $object = (array) $object;
  162.          }
  163.         
  164.          if (is_object($object[0])) {
  165.              if (function_exists("spl_object_hash")) {
  166.                  return spl_object_hash($object[0]).$object[1];
  167.              }else{
  168.                  $class_hash = get_class($object[0]).$object[1];
  169.                 
  170.                  // 如果內部有指定插件的識別碼,則將以 "類別名+識別碼" 返回
  171.                  if (isset($object[0]->filter_id) === true) {
  172.                      $class_hash .= $object[0]->filter_id;
  173.                  }else{                   
  174.                      $class_hash .= isset(self::$plugin_filter[$tag][$priority]) ? count(self::$plugin_filter[$tag][$priority]) : $filter_id_counter;
  175.                      $object[0]->filter_id = $class_hash;
  176.                      ++$filter_id_counter;
  177.                  }
  178.                 
  179.                  return $class_hash;
  180.              }
  181.          }else if ( is_string($object[0]) ) {
  182.              return $object[0].$object[1];    // 物件的靜態呼叫
  183.          }
  184.      }   
  185.     
  186.  }
  187.  ?>
DownLoad: index.php
  1.  <?php
  2.  require_once 'plugin.php';
  3.  
  4.  $content = " This is a test ";
  5.  
  6.  //
  7.  Plugin::init(array("plugin_folder" => "plugin"));
  8.  
  9.  // 增加動作
  10.  Plugin::add_action('get_content', "show_bold_content");
  11.  
  12.  // 基本測試
  13.  Plugin::add_filter("the_content", "trim");
  14.  Plugin::add_filter("the_content", function($value) { 
  15.      return str_replace("test", "TEST", $value);
  16.  }, 10);
  17.  
  18.  // 順序測試
  19.  // (輸出 This is a ABC test -> 受上面的暱名函數影響而變化 -> This is a ABC TEST)
  20.  Plugin::add_filter("the_content", "replace_test_to_abc_test", 1);
  21.  
  22.  // 輸出結果 (filter)
  23.  echo Plugin::apply_filter("the_content", $content);
  24.  echo "<br />";
  25.  
  26.  // 執行動作 (action)
  27.  Plugin::do_action('get_content', $content);
  28.  ?>
DownLoad: get_content.php
  1.  <?php
  2.  function show_bold_content($content) {
  3.      echo "<b>".$content."</b>";
  4.  }
  5.  
  6.  function replace_test_to_abc_test($value) { 
  7.      return str_replace("test", "ABC test", $value);
  8.  }
  9.  ?>

動態提示 (Dynamic Tips)

記錄一下動態增加和顯示提示的代碼.主要特別是:
當不斷加入時,不會即時更新,
當沒有再更新時會自動將已增加的提示一個接一個顯示
最後再顯示回預設的第一個提示

  1.  <script language="javascript" type="text/javascript" src="jquery.js"></script>
  2.  <script language="javascript" type="text/javascript">
  3.  var Tip = {
  4.      default_tips: "",
  5.      tips_store: [],
  6.      timer_id: "",
  7.     
  8.      add_tips_to_store: function(tips) {
  9.          if (this.default_tips == "") {
  10.              this.default_tips = $("div#tips").html();
  11.          }
  12.         
  13.          this.tips_store.push(tips);
  14.         
  15.          if (this.timer_id != "") {
  16.              clearTimeout(this.timer_id);
  17.          }
  18.  
  19.          this.timer_id = setTimeout(function() {
  20.              Tip.update_tips();
  21.          }, 2000);
  22.      },
  23.     
  24.      update_tips: function() {
  25.          //console.log(this.tips_store);
  26.         
  27.          if (this.tips_store.length > 0) {
  28.         
  29.              $("div#tips").fadeOut("slow", function() {
  30.                  var tips = Tip.tips_store.shift();
  31.                  $(this).html(tips).fadeIn("slow", function() {
  32.                      Tip.update_tips();
  33.                  });
  34.              });
  35.         
  36.          }else{
  37.              $("div#tips").fadeOut("slow", function() {
  38.                  $(this).html(Tip.default_tips).fadeIn("slow");
  39.              });
  40.          }
  41.      },
  42.     
  43.      set_tips: function(tips) {
  44.          this.set_default_tips(tips);
  45.          $("div#tips").html(tips);
  46.      },
  47.     
  48.      set_default_tips: function(tips) {
  49.          this.default_tips = tips;
  50.      }
  51.  }
  52.  
  53.  $(document).ready(function() {
  54.      Tip.add_tips_to_store("This is a test 1");
  55.      Tip.add_tips_to_store("This is a test 2");
  56.      Tip.add_tips_to_store("This is a test 3");
  57.      Tip.add_tips_to_store("This is a test 4");   
  58.  })
  59.  </script>
  60.  
  61.  <div id="tips">This is a test Default Message</div>
Page 1 of 2912345»1020...Last »