顯示具有 javascript 標籤的文章。 顯示所有文章
顯示具有 javascript 標籤的文章。 顯示所有文章

2009年7月14日 星期二

色碼表

暑假無聊來寫一些簡單的小程式

這是用javascript來產生色碼表的程式
靈感是來自於以前高中老師所Show的範例

因為怕會消耗太多運算資源,所以Decimal換成Hex Code直接用Array來轉換


<script language="JavaScript">
<!--
var hex = new Array(16)

var i
var j
var k

hex[0] = "00"
hex[1] = "11"
hex[2] = "22"
hex[3] = "33"
hex[4] = "44"
hex[5] = "55"
hex[6] = "66"
hex[7] = "77"
hex[8] = "88"
hex[9] = "99"
hex[10] = "AA"
hex[11] = "BB"
hex[12] = "CC"
hex[13] = "DD"
hex[14] = "EE"
hex[15] = "FF"

document.write('<table border="0">')
document.write('<tr>')
for(i=0;i<16;++i)
{
 document.write('<td>')
 document.write('<table border="0">')
 for(j=0;j<16;++j)
 {
  document.write('<tr>')
  for(k=0;k<16;++k)
  {
  document.write('<td bgcolor="#' + hex[i] + hex[j] + hex[k] + '" width="15" height="15"></td>')
  }
  document.write('</tr>')
  }
  document.write('</table>')
  document.write('</td>')
 
  if(i%4==3)
  {
  document.write('</tr><tr>')
  }
}
document.write('</tr>')
document.write('</table>')

// -->
</script>



結果大概長這樣


怎麼越看越像Die上面的Cache field @@"

按繼續閱讀後有圖有真相


2008年9月1日 星期一

分析網址參數

假設我有一個網址如下:

http://mahdi0828.blogspot.com/search.html?p=1&arg=review&w=false

使用JavaScript內的函數location.search即可取得後面的參數部份

?p=1&arg=review&w=false

把"?"取代成"&"並再後面加一個"&"即變成

&p=1&arg=review&w=false&

以"&"+name+"="搜尋即可知道參數的位置(name為參數名稱)

下一個"="至下一個"&"之間就是參數的值


S1="&"+location.search.substring(1)+"&";

a=S1.search("&"+name+"=")+1; //name是參數名稱

arg=S1.substring(a+S1.substring(a).search("=")+1,a+S1.substring(a).search("&"));



##CONTINUE##

來舉例一下

<script language="javascript">

max=10;
S1="&"+location.search.substring(1)+"&";

if(S1=="&&") { S1="&p=1&"; }

a=S1.search("&"+"p"+"=")+1;
arg=S1.substring(a+S1.substring(a).search("=")+1,a+S1.substring(a).search("&"));

href=location.href.substring(0,location.href.search(location.search.substring(1))-1);

document.write("這是第"+arg+"頁");

if(parseInt(arg)!=1) { document.write("[上一頁]"); }

if(parseInt(arg)!=max) { document.write("[下一頁]"); }

</script>


結果展示

http://alumni.ee.ccu.edu.tw/~hayden0828/example/argument.htm


JS字串處理真的是很強大(看看西佳佳...唉~)

2008年4月24日 星期四

網誌背景日夜交替

研究了好久終於成功了!!

搞這個script搞了一整天,明明只有短短的幾行而已= =。

##CONTINUE##

眼尖的網友應該有發現早上和晚上的背景會不一樣
就是這個的元素的功能!!



把下面這個CODE貼在JAVAScript的網頁元素內就可以了


<!-- title背景日夜切換 start-->
<script language="JavaScript">

Stamp = new Date();

var Hours;
//var Minute;
//var Second;

//取得時間資料
Hours = Stamp.getHours();

//CSS抬頭
document.write('<style type="text/css">');

//判斷日夜
if (Hours >= 6 && Hours < 18)
{
//載入晝背景圖片
document.write('#header-inner {background: url("http://sites.google.com/site/hayden0828/blog/index_d.jpg") right 50%;}');
}
else
{
//載入夜背景圖片
document.write('#header-inner {background: url("http://sites.google.com/site/hayden0828/blog/index_n.jpg") right 50%;}');
}

document.write('</style>');

</script>
<!-- title背景日夜切換 end-->


我其實完全對JAVA外行,
程式碼也是參考很多範例而隨意拼湊出來的。
其實應該對毛玻璃上字的顏色也要同時作變色才對。

至於怎麼製作毛玻璃的效果能參考這篇。


如果有更好的寫法希望能告訴我。