分类目录归档:站长文档

Learning Javascript 3

with "Visual QuickStart Guide javascript and Ajax for the Web, Sixth Edition"

Chapter 3. Language Essentials

Arrays
var newCars = new Array("Toyota", "Honda", "Nissan");
newCars[2] returns "Nissan"

Loop
     for (var i=0; i<24; i++) {
        var newNum = Math.floor(Math.random() * 75) + 1;

        document.getElementById("square"  + i).innerHTML = newNum;
     }

var newNum;
     do {
        newNum = colBasis + getNewNum() + 1;
     }
     while (usedNums[newNum]);
remember that the do block of code always gets executed, whether the while check evaluates to true or false.

switch/case
function saySomething() {
     switch(this.value) {
        case "Lincoln":
           alert("Four score and seven years ago…");
           break;
        case "Kennedy":
           alert("Ask not what your country can do for you…");
           break;
        case "Nixon":
           alert("I am not a crook!");
           break;
        default:
     }
}
we've done everything we want to do, and so we want to get out of the switch. In order to do that, we need to break out. Otherwise, we'll execute all of the code below, too.
The default section is where we end up if our switch value didn't match any of the case values. The default block is optional, but it's always good coding practice to include it, just in case (so to speak).

Detecting Objects
if (document.getElementById) {
 xxxxxxxxxxxxx
}
 else {
        alert("Sorry, your browser doesn't support this script");
}

browser detect
Apple's Safari browser claims that it is a Mozilla browser, even though it is not. And some browsers, such as Safari and Opera, allow you to set which browser you want it to report itself as.
The same goes for attempting to detect which version of javascript a browser supports. We strongly suggest that you do not use these detection methods, and use object detection instead.

try/throw/catch
function initAll() {
     var ans = prompt("Enter a number","");
     try {
        if (!ans || isNaN(ans) || ans<0) {
            throw new Error("Not a valid number");
        }
        alert("The square root of " + ans + " is " + Math.sqrt(ans));
     }
     catch (errMsg) {
        alert(errMsg.message);
     }
}

Once an error is thrown, javascript jumps out of the TRy block and looks for a corresponding catch statement. Everything between here and there is skipped over.
If no error was thrown, the code inside the catch will never be executed.

This's the end of my learning javascript program.

发表在 站长文档 | 留下评论

Learning JavaScript 2

with "Visual QuickStart Guide javascript and Ajax for the Web, Sixth Edition"

Chapter 2. Start Me Up!
Scripts can be put in one of two places on an HTML page: between the <head> and </head> tags (called a header script), or between the <body> and </body> tags (a body script).
        <script language="javascript" type="text/javascript">
          document.write("Hello, world!");
       </script>

internal scripts

external script, a separate file that just contains javascript.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
     <title>My second script</title>
     <script language="javascript" type="text/javascript" src="script02.js">
     </script>
</head>
<body bgcolor="#FFFFFF">
     <h1 id="helloMessage">
     </h1>
</body>
</html>

script02.js:
window.onload = writeMessage;
// Do this when page finishes loading
/*
a function shown with parentheses means that the function is being called, right then and there. When it's without parentheses, (as it is here) we're assigning it to the event handler, to be run later when that event happens.
*/
function writeMessage() {
     document.getElementById("helloMessage"). innerHTML = "Hello, world!";
}

<noscript>
<noscript>
     <h2>This page requires javascript.</h2>
</noscript>

On non-javascript browsers, a message appears saying that this page requires javascript.

confirm() & alert()
if (confirm("Are you sure you want to do that?")) {
     alert("You said yes");
}
else {
     alert("You said no");
}

condition
(condition) ? truePart : falsePart;
like:
myNewVariable = (condition) ?
truevalue : falsevalue;
or
if (condition) {
        truePart;
}
else {
        falsePart;
}

prompt()
var ans = prompt("Are you sure you want to do that?","");
if (ans) {
     alert("You said " + ans);
}
else {
     alert("You refused to answer");
}
 If a variable is created inside a function, other functions don't have access to it, as it's local to that function. If it's created outside any function, it's global, and everything has access to it.

redirect
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
     <title>Welcome to our site</title>
     <script src="script07.js" type="text/javascript" language="javascript">
     </script>
</head>
<body bgcolor="#FFFFFF">
     <h2 align="center">
         <a href="htmlpage.html" id="redirect"> Welcome to our site… c'mon in!</a>
     </h2>
</body>
</html>

window.onload = initAll;
function initAll() {
     document.getElementById("redirect"). onclick = initRedirect;
}
function initRedirect() {
     window.location = "jspage.html";
     return false; //The return false says to stop processing the user's click, so the href page doesn't get loaded.
}
On first glance, we might think that we could just set the onclick handler globallythat is, as the page is loadingbut we can't. There's a chance, particularly for a large and complex page, that the browser will not yet have come across that redirect id, and if that happens, javascript won't be able to assign the onclick handler. Instead, we have to wait until the page has completed loading, and that's done via onload.

action after the user clicks a link
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
      <title>Welcome to our site</title>
      <script src="script08.js" type="text/javascript" language="javascript">
      </script>
</head>
<body bgcolor="#FFFFFF">
      <h2 align="center">
        Hey, check out <a href="
http://www.pixel.mu/" id="redirect">my cat's Web site</a>.
      </h2>
</body>
</html>

window.onload = initAll;

function initAll() {
     document.getElementById("redirect"). onclick = initRedirect;
}

function initRedirect() {
     alert("We are not responsible for the content of pages outside our site");
     window.location = this; //THIS!!!
  /*The javascript keyword this allows the script to pass a value to a function, solely based on the context where the keyword is used.*/
     return false;
}

document.referrer
A referrer page is the page that the user was viewing before the current page, or, in other words, the page the user came from.
function writeMessage() {
     if (document.referrer != "") {
         document.getElementById("referrerMsg"). innerHTML = "I hope you like this page
better than " + document.referrer;
     }
}

Unobtrusive scripting: An approach to scripting Web pages using javascript, in which the behavior of the Web page is kept separate from its content, that is, the HTML is in one file, and the javascript is in another. Additionally, the term "unobtrusive scripting" is used when code is written such that visitors without javascript (or with less-capable browsers) get all the functionality of a site, just with a less-rich user experience.

And finally, this being the real world, we also know that sometimes the simplest way to hammer in a nail is to grab a rock and pound the nail into the wall. This, for instance, is why we used innerHTML back in Script 2.3, even though it's not part of the W3C DOM.

发表在 站长文档 | 留下评论

Learning Javascript

with "Visual QuickStart Guide javascript and Ajax for the Web, Sixth Edition"
By Tom Negrino, Dori Smith
target browsers:
Internet Explorer 6 or later; Firefox 1.0 or later; Netscape 7 or later; all versions of Safari; and Opera 7 or later.

Chapter 1. Getting Acquainted with javascript

What javascript Is
Despite the name, javascript and Java have almost nothing to do with one another.

With Java, a descendant of the C and C++ programming languages, programmers can create entire applications and control consumer electronic devices.
Microsoft dropped Sun's Java from Windows altogether, after creating its own Java-like language, C#.
If javascript isn't related to Java, then why do they have such similar names? It's another example of one of the computer industry's most annoying traits: the triumph of marketing over substance.
and ever since then, writers like us have made good money explaining that javascript and Java are very different things.
This Microsoft version of javascript is called JScript.

What javascript Can Do
 A rollover is an image that changes when you move the mouse pointer over it.
javascript has some limitations built-in, mostly for security reasons:
 1.javascript does not allow the reading or writing of files on client machines. The only exception is that javascript can write to the browser's cookie file.
 2.javascript does not allow the writing of files on server machines.
 3.javascript cannot close a window that it hasn't opened.
 4.javascript cannot read information from an opened Web page that came from another server.

What Is Ajax?
Ajax is shorthand for Asynchronous javascript and XML.
In the larger scheme of things, what's generally referred to as Ajax is the combination of these technologies:
 XHTML
 CSS (Cascading Style Sheets)
 The DOM (Document Object Model) accessed using javascript
 XML, the format of the data being transferred between the server and the client
 XMLHttpRequest to retrieve data from the server

javascript is an object-oriented language
Objects
Properties, Objects have properties.
Methods, The things that objects can do are called methods.
dot syntax
 document.images.name
 forms.elements.radio.click()

The representation of the objects within the document is called the Document Object Model (DOM).
Each of the objects on the tree is also called a node of the tree. If the node contains an HTML tag, it's referred to as an element node. Otherwise, it's referred to as a text node.
Events are actions that the user performs while visiting your page. javascript deals with events using commands called event handlers.
The 12 most common javascript event handlers :
 onabort
 onblur
 onchange
 onclick
 onerror
 onfocus
 onload
 onmouseover
 onmouseout
 onselect
 onsubmit
 onunload

value Types:
 Number
 String
 Boolean
 Null
 Object
 Function
value returned by a function
Variable names cannot contain spaces or other punctuation, or start with a digit. They also can't be one of the javascript reserved words.  javascript is case sensitive.
Operators:
 x + y (Numeric)
 x + y (String)
 x – y
 x * y
 x / y

 x % y Modulus of x and y (i.e., the remainder when x is divided by y)
 x++, ++ x
 (if x is 5, y=x++ results in y set to 5 and x set to 6, while y=++x results in both x and y set to 6. The operator – (minus sign) works similarly.)
 x–, –x
 -x

Assignments:
 x = y
 x += y

  Same as x = x + y
 x -= y
 x *= y
 x /= y
 x %= y

Comparisons:
 x == y
 x != y
 x > y

 If you are comparing strings, be aware that "a" is greater than "A" and that "abracadabra" is less than "be".
 x > = y
 x < y
 x <= y
 x && y
 x || y
 !x

Writing javascript-Friendly HTML
Structure, presentation, and behavior
When split up this way, your sites will contain three types of text files:
 XHTML: content and structure
 CSS: appearance and presentation
 javascript: behavior
CSS:
A <div> is a block-level element, that is, there's a physical break between it and the elements above and below it. A <span> isn't block-level; instead, it's inline, so you can apply it to, for instance, a single phrase within a sentence.
A class identifies an element that you may want to use more than once. An id identifies an element that is unique to that document.

发表在 站长文档 | 留下评论

如何将使用Access数据库文件的ASP程序转向SQL数据库

如何将使用access数据库文件的asp程序转向sql数据库

因为blog的access数据库文件经常出问题, 于是昨晚将blog转向了sql数据库. 网上找了一些文章, 现在经合自己的经验, 把实际操作要点写下来.

一.数据库转换问题
1.用SQL Server新建一个数据库, 然后将mdb文件中的access数据导入(import)这个新建的数据库.
2.access数据库原来自动增长的ID在SQL中要重新设置,设置为“标识”(identity).
3.access表内数据类型"是/否",到了sql server的表内就是 bit (not null),有可能一些true/false类型不能使用,要变为1/0。
4.原来的默认值都丢失了.主要是数字类型和日期类型.
5.转化时,跟日期有关的字段,SQL SERVER默认为smalldatetime型,我们最好将它变为datetime型,因为datetime型的范围比smalldatetime型大。有时用smalldatetime型时,转化失败,而用datetime型时,转化成功。
6.在mssql server中,有许多保留字。mssql在导入的时候,会自动给这些字段(包括数据库中的表名)加上[字段名],因此,你必须修改你的脚本,把相应的字段名字(或者表名字)加上中括号,或改变字段名字为不是mssql的保留字.最好在有可能和系统关键字的地方使用[和]将他包围起来,以避免在移植过程中出现的运行错误问题  
7.CursorType要改成1,也就是打开数据库时要给出第一个数字参数为1,否则记录可能 显示不完整 
8.备注类型要通过cast(column as varchar)来使用 

二.asp数据库连接语句修改
以下假设strConn为连接字符串变量名.
由原来strConn="Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(数据库文件路径)
改为:strConn="Provider=SQLOLEDB;Data Source=’数据库所在服务器IP(本地为127.0.0.1)’;Initial Catalog=’数据库名’;User ID=’数据库用户名’;Password=’数据库用户密码’;" 

三.查寻语句问题(注意以下适用的是sql查寻语句中的修改原则, 不要修改查寻语句外的asp语句)
1.所有的sql语句中的now(),time(),date()必须换成getdate()。
2.isnull(rowname)要改成rowname = null 
3.所有datediff(’d’, time1, time2)要改成datediff(day, time1, time2) ,所有datediff(’ww’, time1, time2)要改成datediff(week, time1, time2)  (或datediff("d", time1, time2)要改成datediff(day, time1, time2) ,datediff("ww", time1, time2)要改成datediff(week, time1, time2)  )
4.“DELETE * FROM ……”要改为“DELETE FROM ……” 例如:在对ACCESS数据库进行删除纪录时用:"delete * from user where id=10",而对SQL SERVER数据库进行删除是用:"delete user where id=10". 
5.access里面除法可以使用"\"或者"/",MSSQL里面只能使用"/"
6.SQL语句中Like后面字符串的通配符’*'要改成’%'。
7.access查询通过cdate构造日期,sql server内是convert(datetime,…), convert使用方法见下文.
8.在对ACCESS数据库处理中,sql语句中直接可以用一些VB的函数,像instr()函数,而对SQL SERVER数据库处理中不能使用这类函数(instr()可用charindex函数代替,方法见下文)。 
9.在access的sql语句中的时间使用变量查询的时候,一般使用"select * from aaaa while time=#"&变量名&"#",在mssql中的语法是“select * from aaaa while time=’"&变量名&"’"”。不然有可能rs.update失败,修改成update 表名 set 字段=‘值’ 这样通过(遇到的情况,提示为: Microsoft OLE DB Provider for SQL Server 错误 ’80040e38’  )

SQL中CONVERT转化函数的用法

CONVERT的使用方法:

////////////////////////////////////////////////////////////////////////////////////////

格式:
CONVERT(data_type,expression[,style])

说明:
此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)
相互转换的时候才用到.

例子:
SELECT CONVERT(varchar(30),getdate(),101) now
结果为
now
—————————————
09/15/2001

/////////////////////////////////////////////////////////////////////////////////////

style数字在转换时间时的含义如下

————————————————————————————————-
Style(2位表示年份) | Style(4位表示年份) | 输入输出格式 
————————————————————————————————-
- | 0 or 100 | mon dd yyyy hh:miAM(或PM) 
————————————————————————————————-
1 | 101 | mm/dd/yy 
————————————————————————————————-
2 | 102 | yy-mm-dd 
————————————————————————————————-
3 | 103 | dd/mm/yy 
————————————————————————————————-
4 | 104 | dd-mm-yy 
————————————————————————————————-
5 | 105 | dd-mm-yy 
————————————————————————————————-
6 | 106 | dd mon yy 
————————————————————————————————-
7 | 107 | mon dd,yy 
————————————————————————————————-
8 | 108 | hh:mm:ss 
————————————————————————————————-
- | 9 or 109 | mon dd yyyy hh:mi:ss:mmmmAM(或PM)
————————————————————————————————-
10 | 110 | mm-dd-yy 
————————————————————————————————-
11 | 111 | yy/mm/dd 
————————————————————————————————-
12 | 112 | yymmdd 
————————————————————————————————-
- | 13 or 113 | dd mon yyyy hh:mi:ss:mmm(24小时制) 
————————————————————————————————-
14 | 114 | hh:mi:ss:mmm(24小时制) 
————————————————————————————————-
- | 20 or 120 | yyyy-mm-dd hh:mi:ss(24小时制, 应该和now()生成的日期格式相同) 
————————————————————————————————-
- | 21 or 121 | yyyy-mm-dd hh:mi:ss:mmm(24小时制) 
————————————————————————————————-

 

使用charindex函数代替   
    
  CHARINDEX   
  返回字符串中指定表达式的起始位置。     
    
  语法   
  CHARINDEX   (   expression1   ,   expression2   [   ,   start_location   ]   )     
    
  参数   
  expression1   
    
  一个表达式,其中包含要寻找的字符的次序。expression1   是一个短字符数据类型分类的表达式。   
    
  expression2   
    
  一个表达式,通常是一个用于搜索指定序列的列。expression2   属于字符串数据类型分类。   
    
  start_location   
    
  在   expression2   中搜索   expression1   时的起始字符位置。如果没有给定   start_location,而是一个负数或零,则将从   expression2   的起始位置开始搜索。   
    
  返回类型   
  int   
    
  注释   
  如果   expression1   或   expression2   之一属于   Unicode   数据类型(nvarchar   或   nchar)而另一个不属于,则将另一个转换为   Unicode   数据类型。   
    
  如果   expression1   或   expression2   之一为   NULL   值,则当数据库兼容级别为   70   或更大时,CHARINDEX   返回   NULL   值。当数据库兼容级别为   65   或更小时,CHARINDEX   仅在   expression1   和   expression2   都为   NULL   时返回   NULL   值。     
    
  如果在   expression2   内没有找到   expression1,则   CHARINDEX   返回   0。   

发表在 站长文档 | 留下评论