2008/10/28

javascript: 以javascript取得query string (Netlobo.com | Get URL Parameters Using Javascript)

http://www.netlobo.com/url_query_string_javascript.html


function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}

var myid= gup("id");

PowerShell : 如何使用PowerShell呼叫WebRequest?

節錄出程式內的function應該就能了解了:


function GoRefresh($gdString)
{
$today = Get-Date
write-host "更新:" $id "-->" $today

$url = "http://192.168.1.100/refresh_html.ashx?id=" + $id
write-host "url:" $url

$request = [System.Net.WebRequest]::Create($url)

$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream = new-object System.IO.StreamReader $requestStream
new-variable tmp
$tmp = $readStream.ReadToEnd()
write-host "response:" $tmp
$readStream.Close()
$response.Close()
}

參考來源:Basic Powershell examples, couple useful commands

PowerShell : 運算子說明

PowerShell 運算子的說明:

運算子 說明
-eq 相等 (大寫或小寫均可)
-lt 小於(大寫或小寫均可)
-gt 大於(大寫或小寫均可)
-le 小於等於(大寫或小寫均可)
-ge 大於等於(大寫或小寫均可)
-ne 不等於 (大寫或小寫均可)
-not 邏輯運算的反向運算 (亦可以 ! 表示)
-match 使用規則運算式比較字串
-notmatch 使用規則運算式比較字串
-like 使用萬用字元比較字串
-notlike 使用萬用字元比較字串
-replace 替換字串(如果執行替換且傳回True)
-and 邏輯 and 運算
-or 邏輯 or 運算
-bor 位元 OR 運算
-band 位元 AND 運算
-xor XOR 運算子
-comp 反向運算子 (~)
-sr 向右位移
-sl 向左位移
-ceq 相等 (必須都是小寫)
-clt 小於 (必須都是小寫)
-cgt 大於 (必須都是小寫)
-cle 小於等於 (必須都是小寫)
-cge 大於等於 (必須都是小寫)
-cne 不等於 (必須都是小寫)
-ieq 等於 (大寫或小寫均可)
-ilt 小於 (大寫或小寫均可)
-igt 大於 (大寫或小寫均可)
-ige 大於等於 (大寫或小寫均可)
-ine 不等於 (大寫或小寫均可)
-inot 邏輯運算的反向運算 (大寫或小寫均可)
-imatch 使用規則運算式比較字串 (大寫或小寫均可)
-ilike 使用萬用字元比較字串 (大寫或小寫均可)
-inotlike 使用萬用字元比較字串 (大寫或小寫均可)


2008/10/27

PowerShell : 換行符號

以下範例是將dataset一行一行的存入text file:


function SaveTxtBody($ds)
{
$txtBody = ""

foreach ($item in $ds.Tables[0].Rows)
{
$line = ""

for ($index = 0; $index -lt $ds.Tables[0].Columns.Count; $index++) {

$line += $item[$index]
if (!($index -eq ($ds.Tables[0].Columns.Count -1)))
{
$line += ","
}
}

$txtBody += $line
$txtBody += [char]13+[char]10 # 換行符號
}

$txtBody > $pathSampleFile
}

2008/10/23

TomTom也有自拍功能?!

今天逛Mobile01時看到的,回去拿車子裡的TomTom One XL來試試...

1. 先在TomTom的根目錄開一個"screen"的資料夾
2. 在"screen"資料夾內,新增一個無副檔名的檔案,名為"capture"
3. 重開機,按螢幕的左上方角落,聽到「咔嚓」的照相機聲即是擷圖成功

資料來源:Mobile01-TomTom 也會自拍哦!

2008/10/22

javascript : jquery ajaxStart and ajaxStop 的用法

ajaxStart和ajaxStop主要是在頁面觸發ajax時,呼叫其他function或程式碼做處理。
要加在$(document).ready(function(){});內
$().ajaxStart(function() {
$.blockUI.defaults.pageMessage = "<table class='loader' summary='Loader Layout' border='0' cellpadding='5' cellspacing='1' width='300px'><tr><td align='left' valign='baseline'><div id='innLoader' style='position: absolute; z-index: 999; border-right: #bbbbbb thin solid; border-top: #bbbbbb thin solid; border-left: #bbbbbb thin solid; border-bottom: #bbbbbb thin solid; width: 300px; background-color: white;'><table><tr><td><img src='/res/layout/v5/ps_loading.gif' width='80' height='80' align='left' style='margin: 3px;' alt='請稍候' /></td><td><p><strong>資料處理中, 請稍候....</strong></p></td></tr></table></div></td></tr></table>";
$.extend($.blockUI.defaults.pageMessageCSS, { margin: '-50px 0 0 -125px', top: '50%', left: '50%', color: '#000', backgroundColor: '#fff', border: '0px solid #aaa' }); $.blockUI();
}).ajaxStop($.unblockUI);

2008/10/21

javascript: string to JSON


string myJSONText = "{ /"name/", /"jessewang/", /"age/", /"18/"}";
var myObject = eval('(' + myJSONtext + ')');

如果不想使用eval這個函數,可以下載JSON Parser來使用會比較安全。
下載: The open source code of a JSON parser and JSON stringifier is available.
參考: JSON in JavaScript

下載後,include到html裡,使用以下指令即可:


var myObject = JSON.parse(myJSONtext);


PANERAI PAM00000  Luminor 經典手上鍊腕錶-44mm

PowerShell 如何在command line執行?


powershell c:\test\mypowershell_test.ps1

或是

powershell ./mypowershell_test.ps1


要把powershell放到排程裡,可用.bat檔把以上指令包起來即可。

PowerShell 安全性的設定

因為安全性設定的關係,在預設ps1 script無法執行,請在PowerShell Console執行以下指令調整安全性:

Set-ExecutionPolicy RemoteSigned

還原本的設定:

Set-ExecutionPolicy Restricted

其他的設定請參考:

What Can I Do With Windows PowerShell?

2008/10/20

Vista開啟防火牆後,設定遠端桌面可連線

一般來說,防火牆打開後,我們都會在防火牆的例外裡打開terminal service的port 3389



但告訴各位,這樣是行不通的!
重開機後又會讓你的terminal service無法連線...

解法是: 開始-->執行-->輸入 gpedit 後,打開群組原則物件編輯器
展開系統管理範本-->網路-->網路連線-->Windows防火牆-->網域設定檔, 將Windows防火牆:允許輸入的遠端桌面例外設定成已啟用

系統管理範本-->網路-->網路連線-->Windows防火牆-->標準設定檔, 將Windows防火牆:允許輸入的遠端桌面例外設定成已啟用


這樣重開機terminal service也可以連線了。

2008/10/19

LINQ 語法簡介

介紹幾個基本的LINQ語法

LinqSample.aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinqSample.aspx.cs" Inherits="LinqSample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>LINQ Sample</title>
<style type="text/css">
.style1
{
font-size: xx-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<span class="style1">Linq Sample</span><br />
<br />
<asp:Button ID="ButtonFromAndSelect" runat="server" Text="select"
onclick="ButtonFromAndSelect_Click" />
<asp:Button ID="ButtonWhere" runat="server" Text="where"
onclick="ButtonWhere_Click" />
<asp:Button ID="ButtonForeach" runat="server" Text="foreach"
onclick="ButtonForeach_Click" />
<asp:Button ID="ButtonSelectNew" runat="server" Text="select new"
onclick="ButtonSelectNew_Click" />
<asp:Button ID="ButtonOrderBy" runat="server" Text="orderby"
onclick="ButtonOrderBy_Click" />
<asp:Button ID="ButtonTake" runat="server" Text="take"
onclick="ButtonTake_Click" />
<asp:Button ID="ButtonSkipAndTake" runat="server" Text="skip and take"
onclick="ButtonSkipAndTake_Click" />
<asp:Button ID="ButtonInsert" runat="server" Text="insert"
onclick="ButtonInsert_Click" />
<asp:Button ID="ButtonUpdate" runat="server" Text="update"
onclick="ButtonUpdate_Click" />
<asp:Button ID="ButtonDelete" runat="server" Text="delete"
onclick="ButtonDelete_Click" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>


LinqSample.aspx.cs


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class LinqSample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
// LINQ : Select
protected void ButtonFromAndSelect_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select pt;
GridView1.DataSource = dt;
GridView1.DataBind();

}
// LINQ : Where
protected void ButtonWhere_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
where pt.ProductID == 1
select pt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
// LINQ : Foreach
protected void ButtonForeach_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select pt;

foreach (var item in dt)
{
Response.Write("product id:" + item.ProductID.ToString() + "<br>");
}
}
// LINQ : Select New
protected void ButtonSelectNew_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select new
{
ProductId = pt.ProductID,
ProductName = pt.ProductName
};
GridView1.DataSource = dt;
GridView1.DataBind();
}
// LINQ : Orderby
protected void ButtonOrderBy_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
orderby pt.ProductID descending
select pt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
// LINQ : Take
protected void ButtonTake_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select pt;
GridView1.DataSource = dt.Take(5);
GridView1.DataBind();
}
// LINQ : Skip and Take
protected void ButtonSkipAndTake_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();
var dt = from pt in northwind.Products
select pt;
GridView1.DataSource = dt.Skip(10).Take(5);
GridView1.DataBind();
}
// LINQ : InsertOnSubmit
protected void ButtonInsert_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();

Products newPt = new Products();
newPt.ProductName = "iphone";
newPt.SupplierID = 5;
newPt.CategoryID = 4;
newPt.QuantityPerUnit = "10 - 500 g pkgs.";
newPt.UnitPrice = 25000;
newPt.UnitsInStock = 80;
newPt.UnitsOnOrder = 0;
newPt.ReorderLevel = 0;
newPt.Discontinued = false;
northwind.Products.InsertOnSubmit(newPt);
northwind.SubmitChanges();

var dt = from pt in northwind.Products
orderby pt.ProductID descending
select pt;

GridView1.DataSource = dt;
GridView1.DataBind();
}
// LINQ : Update
protected void ButtonUpdate_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();

Products pt = northwind.Products.Single(currPt => currPt.ProductName == "iphone");
pt.ProductName = "iPhone 3G";
northwind.SubmitChanges();
}
// LINQ : Delete
protected void ButtonDelete_Click(object sender, EventArgs e)
{
NorthwindDataContext northwind = new NorthwindDataContext();

Products pt = northwind.Products.Single(currPt => currPt.ProductName == "iPhone 3G");
northwind.Products.DeleteOnSubmit(pt);
northwind.SubmitChanges();
}
}

2008/10/18

字串比對上的眉角



if (e.Row.Cell[0].Text.Equals("mytest"))

...

改成



if ("mytest".Equals(e.Row.Cell[0].Text))

...

可避免因為e.Row.Cell[0].Text如果是空值而造成程式exception.

2008/10/13

jquery: 取出指定form內的所有select物件

在myform裡取出所有select name 開頭是'att'的值:

$("form[name='myform'] select[name^='att']").each(function(){
alert($(this).val());
});

判斷瀏覽器是否為Google Chrome的二種方法

1. 使用jquery,但要先加入一個jquery plugin, 可以以下連結下載:

jQuery Browser Plugin

下載後,include到你的html裡,然後直接下指令即可:


if ($.browser.name == "chrome")
alert("google chrome!");


2. 直接判斷navigator.userAgent裡的文字, 請參考:Detecting Google Chrome Using Javascript


var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

軟體工程的重要的指標