顯示具有 IT - JavaScript 標籤的文章。 顯示所有文章
顯示具有 IT - JavaScript 標籤的文章。 顯示所有文章

2009年10月7日 星期三

Cookie Handling Routines in JavaScript

These routines are not rocket science, but they do provide a complete set of easy-to-use cookie handling routines in JavaScript. You will find below demonstrations of all the routines, together with the calling sequences used.

(Session Cookies + Persistent Cookies)

The following routines are provided in the zip file:

1) Test to see if session cookies are enabled.
2) Test to see if persistent cookies are enabled.
3) Write a session cookie
4) Write a persistent cookie
5) Get value of a named cookie
6) Delete a persistent cookie

URL : http://www.braemoor.co.uk/software/cookies.shtml

2009年9月10日 星期四

JavaScript: Regular Expression on Domains (2 methods)

Method 1:

//-- section = 0 : index+sub-index, 1 : content --//
var section="0";
var str = document.location.href;

var url_pattern=new Array(
"^news.sina.com.hk$",
"^news.sina.com.hk/$",
"^news.sina.com.hk/index.html$",
"^/cgi-bin/nw/realtime.cgi$",
"^/cgi-bin/nw/main.cgi[?]cat=2$",
"^/cgi-bin/nw/main.cgi[?]cat=19$",
"^/cgi-bin/nw/international.cgi$",
"^/cgi-bin/nw/china.cgi$",
"^/cgi-bin/nw/main.cgi[?]cat=15$",
"^/cgi-bin/nw/main.cgi[?]cat=22$",
"^/cgi-bin/nw/main.cgi[?]cat=23$",
"^/cgi-bin/vote/index.cgi$",
"^/cgi-bin/nw/special.cgi$"
);

for(var i=0; i{
if(new RegExp(url_pattern[i]).test(str))
{
section="0";
}
else
{
section="1";
}
}

if(section=="0") {
alert("index");
}
else {
alert("content");
}



Method 2:

var as_section='ros'; //all page are ros except match the following
var as_curr_url = document.location.href;
as_curr_url= as_curr_url.replace(RegExp("^http://"+location.hostname), '');

var as_exact_match = new Array(
"/",
"/index.html",
"/cgi-bin/nw/realtime.cgi",
"/cgi-bin/nw/main.cgi?cat=2",
"/cgi-bin/nw/main.cgi?cat=19",
"/cgi-bin/nw/international.cgi",
"/cgi-bin/nw/china.cgi",
"/cgi-bin/nw/main.cgi?cat=15",
"/cgi-bin/nw/main.cgi?cat=22",
"/cgi-bin/nw/main.cgi?cat=23",
"/cgi-bin/vote/index.cgi",
"/cgi-bin/nw/special.cgi"
);

var as_pattern = new Array(
//"/test/regex.html$"
);

for ( var i = 0; i < as_exact_match.length; i++) {
if (as_exact_match[i] == as_curr_url) {
as_section='index'; break;
}
}

if (as_section == "ros"){
for ( var i = 0; i < as_pattern.length; i++) {
if (as_curr_url.match(as_pattern[i])) {
as_section='index'; break;
}
}
}

if(as_section == 'index'){
alert("index");
}else {
alert("content");
}

JavaScript: detect browser width and align layer to center

Equation:

var x = (document.body.clientWidth/2) - 364;

P.S. Banner Width (728)/ 2 = 364

Demo: http://ads.sina.com.hk/demo/200909/sample20090909_ET_exBanner4.html