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.

發表在 站長文檔 | 留下評論

一個都不能少 1999

一個都不能少 1999

http://www.imdb.com/title/tt0209189/

感覺太平淡了。 這也是這部電影的特色。 電影情節平淡如水。 沒有電腦特效, 沒有動作特技;再加上沒有一個專業演員, 電影的畫面平淡如水。說是電影,但更像是偷拍下來的記錄片。
唯一讓我不平的是那慢慢發現的平淡中夾着的些許讓人感動的執着。
7

發表在 電影評論 | 留下評論

那天我經過關公廟

聽到上面這個故事, 我想起一件類似的事來。
那事發生得有一段時間了。。。屈指一算,也有二十來年了。 
當年我們華山和嵩山一起去攻打金刀門, 不想中了金刀門的埋伏,被殺得死傷無數。亂戰中我正想找個清靜點的地方躲藏, 撞進了金刀門後院的一間小屋,不想發現掌門夫人和慧明方丈也呆在那裡。方丈躺在屋角的草堆上,掌門夫人正哭泣着在給他包紮大腿上的傷口。我正想迴避出去,可沒想到不由我分說,掌門夫人過來一把抓住我的衣領,要我拿着一隻玉手鐲回華山叫她女兒帶着鳳凰笛子過來。
我沒弄懂怎麼回事, 就被劉掌門夫人扔出了後院高牆。我很高興,因為院子裡面刀光劍影,實在太亂了,而我是個喜歡清靜的人。在裡面時一直想出去,可惜那牆我爬不上去。
現在好了,現在可以回華山了。來時我正在讀着詩集烤白薯,匆忙間被召集去攻打金刀門,而現在正好順道回去看看白薯烤焦了沒有了。
而且是一個人。太好了。來時一大幫,吵得很。一邊走,掌門還讓我們一邊唱華山之歌,”華山,華山,好大的華山。。。“。那歌詞沒有詩意不要緊,而且不壓韻,我一聽就發暈,而且還要我唱得比嵩山弟子的“少林頌”大聲,噁心死我了。現在好了。這二百多里路我可以一個人走了。
當時正是5月,山花爛漫。我想寫詩。”五月花開一大山“,可就是一直想不出下句。也許是因為最近沒有愛情的原因。
我是在經過清風寨時才發現自己迷了路的。
當時我進寨子路邊的一酒店吃飯。我依舊點了三個包子。結賬時小二要了我10文。我跟小二說理,他說這裡的包子一直是10文三個,不是9文, 而且他說他們的店名一直是“豐裕酒店”,沒有叫過“德康飯莊”。在找了半天,沒有找到我那日題有“老鼠愛大米”字樣的飯桌後我終於相信了他的話。也就是說華山在北面,而我向南跑了一天。
但事後證明我走的方向總體來說應該是對的。
也就因這些機緣巧合,第二天中午我才經過了陸家溝附近的那個關公廟,才看到那件我一生也不會忘記事情

正文待續。。。

發表在 蓋世武功 | 一條評論

天下第一劍 1968

天下第一劍 1968

http://www.imdb.com/title/tt0062959/

故事發生的南北劍都還在的時候。 當時有個小姐妝化得很有特色。

最後的決戰很無奈, 當時程小東,徐克都還沒上場。 畫面很單調, 兩位高手竟然都靠瞪眼睛拖時間, 後面又加入了慢動作敷衍。 我很着急, 怕他們互相抓起頭髮來。但這事終沒有發生, 於是畫面始終都是單調的。

4

發表在 電影評論, 蓋世武功 | 留下評論

馬背上的法庭

馬背上的法庭

看這部片我更多的是想回味一下雲南那連綿不斷的大山。
沒想到在裡面還看到多少少數民族的生活。

6

發表在 電影評論 | 留下評論

美麗的大腳 2002

美麗的大腳 2002

http://www.imdb.com/title/tt0361634/

女主角的獻身教育鏡頭不是很多, 更多的是她的偷情事迹, 不明白這有什麼讓女配角歇斯底里地感動流淚的。
影片做作的場面太多了, 讓人看了反感。

5

發表在 電影評論 | 留下評論

暖春 2004

暖春 2004

《暖春》是一部很缺心眼的電影。它專攻人的弱點,完全不留任何情面,招招致命,並且一劍剛過一掌又出,完全不給人喘氣機會。
我懷疑這個沒有半點大俠風範的導演烏蘭塔娜是專攻眼科醫學的。而和山西電影製片廠一起參加了《暖春》拍攝工作的另一個位單,XX貿易公司實際上很有可能是一家紙巾代理商。
是的,雖然知道他們是預謀好的,但觀看影片你還是不得不準備一卷足本的紙巾。為防止體液流失過多而導致休克,請注意及時補充身體水分。

8

發表在 電影評論 | 留下評論

鷹爪鐵布衫 1977

鷹爪鐵布衫 1977

http://www.imdb.com/title/tt0076939/

7

前段時間看了《獨臂刀》 後想看兒時看過的經典武打電影《鷹爪鐵布衫》。 想了好多天, 今天終於看了。

《鷹爪鐵布衫》 的確是一部很不錯的武俠電影。 電影開頭的武功說明和電影后段的穴位介紹圖在今天看來很有特色。 電影的故事也不錯, 雖然世界小了點。

和今天的電影相比, 當時的打鬥並不落後。 演員大多是練過的, 一招一招都顯剛勁有神。 不像現在的電影, 周杰倫一旦須要也能成為武林高手。

以前管這個電影叫武打電影, 今天發覺應該是武俠電影。 裡面有兩個大俠, 和一少俠,小女俠。我突然發現比起當年, 現在的武俠電影少得可憐。 最近幾年拍的好像只有《卧虎藏龍》算得上是一部武俠電影。 像
《英雄》沒有什麼江湖可言, 俠也不是體現的重點, 到了〈滿城盡帶黃金甲 〉裡面乾脆沒有一個俠了,更談不上江湖了。

雖然《鷹爪鐵布衫》是部不錯的電影,但我發現我看錯了。 這部別名可以叫《兩個雞蛋》的電影並不是我多天來想看的那部。 我記錯了。 或《鷹爪鐵布衫》從小我沒有沒有完整看完過。 而我真正想看的電影應該是《無敵鴛鴦腿》。。。

發表在 電影評論, 蓋世武功 | 一條評論

US States List for Ruby on Rails

def select_state_for_user
       [["","Select State..."], ["AL","Alabama"],
        ["AK","Alaska"], ["AZ","Arizona"], ["AR","Arkansas"],
        ["CA","California"], ["CO","Colorado"], ["CT","Connecticut"],
        ["DE","Delaware"], ["DC","District of Columbia"],
["FL","Florida"],

        ["GA","Georgia"], ["HI","Hawaii"], ["ID","Idaho"],
["IL","Illinois"],
        ["IN","Indiana"], ["IA","Iowa"], ["KS","Kansas"],
["KY","Kentucky"],
        ["LA","Louisiana"], ["ME","Maine"], ["MD","Maryland"],
        ["MA","Massachusetts"], ["MI","Michigan"], ["MN","Minnesota"],
        ["MS","Mississippi"], ["MO","Missouri"], ["MT","Montana"],
        ["NE","Nebraska"], ["NV","Nevada"], ["NH","New Hampshire"],
["NJ","New
        Jersey"], ["NM","New Mexico"], ["NY","New York"], ["NC","North
        Carolina"], ["ND","North Dakota"], ["OH","Ohio"],
["OK","Oklahoma"],
        ["OR","Oregon"], ["PA","Pennsylvania"], ["RI","Rhode Island"],
        ["SC","South Carolina"], ["SD","South Dakota"],
["TN","Tennessee"],
        ["TX","Texas"], ["UT","Utah"], ["VT","Vermont"],
["VA","Virginia"],
        ["WA","Washington"], ["WV","West Virginia"], ["WI","Wisconsin"],
        ["WY","Wyoming"]]
end

STATES = [
    [ "Alabama", "AL" ],
    [ "Alaska", "AK" ],
    [ "Arizona", "AZ" ],
    [ "Arkansas", "AR" ],
    [ "California", "CA" ],
    [ "Colorado", "CO" ],
    [ "Connecticut", "CT" ],
    [ "Delaware", "DE" ],
    [ "Florida", "FL" ],
    [ "Georgia", "GA" ],
    [ "Hawaii", "HI" ],
    [ "Idaho", "ID" ],
    [ "Illinois", "IL" ],
    [ "Indiana", "IN" ],
    [ "Iowa", "IA" ],
    [ "Kansas", "KS" ],
    [ "Kentucky", "KY" ],
    [ "Louisiana", "LA" ],
    [ "Maine", "ME" ],
    [ "Maryland", "MD" ],
    [ "Massachusetts", "MA" ],
    [ "Michigan", "MI" ],
    [ "Minnesota", "MN" ],
    [ "Mississippi", "MS" ],
    [ "Missouri", "MO" ],
    [ "Montana", "MT" ],
    [ "Nebraska", "NE" ],
    [ "Nevada", "NV" ],
    [ "New Hampshire", "NH" ],
    [ "New Jersey", "NJ" ],
    [ "New Mexico", "NM" ],
    [ "New York", "NY" ],
    [ "North Carolina", "NC" ],
    [ "North Dakota", "ND" ],
    [ "Ohio", "OH" ],
    [ "Oklahoma", "OK" ],
    [ "Oregon", "OR" ],
    [ "Pennsylvania", "PA" ],
    [ "Rhode Island", "RI" ],
    [ "South Carolina", "SC" ],
    [ "South Dakota", "SD" ],
    [ "Tennessee", "TN" ],
    [ "Texas", "TX" ],
    [ "Utah", "UT" ],
    [ "Vermont", "VT" ],
    [ "Virginia", "VA" ],
    [ "Washington", "WA" ],
    [ "West Virginia", "WV" ],
    [ "Wisconsin", "WI" ],
    [ "Wyoming", "WY" ]
  ].freeze

 STATES = [
    [ "", "" ],
    [ "Alabama", "Alabama" ],
    [ "Alaska", "Alaska" ],
    [ "Arizona", "Arizona" ],
    [ "Arkansas", "Arkansas" ],
    [ "California", "California" ],
    [ "Colorado", "Colorado" ],
    [ "Connecticut", "Connecticut" ],
    [ "Delaware", "Delaware" ],
    [ "Florida", "Florida" ],
    [ "Georgia", "Georgia" ],
    [ "Hawaii", "Hawaii" ],
    [ "Idaho", "Idaho" ],
    [ "Illinois", "Illinois" ],
    [ "Indiana", "Indiana" ],
    [ "Iowa", "Iowa" ],
    [ "Kansas", "Kansas" ],
    [ "Kentucky", "Kentucky" ],
    [ "Louisiana", "Louisiana" ],
    [ "Maine", "Maine" ],
    [ "Maryland", "Maryland" ],
    [ "Massachusetts", "Massachusetts" ],
    [ "Michigan", "Michigan" ],
    [ "Minnesota", "Minnesota" ],
    [ "Mississippi", "Mississippi" ],
    [ "Missouri", "Missouri" ],
    [ "Montana", "Montana" ],
    [ "Nebraska", "Nebraska" ],
    [ "Nevada", "Nevada" ],
    [ "New Hampshire", "New Hampshire" ],
    [ "New Jersey", "New Jersey" ],
    [ "New Mexico", "New Mexico" ],
    [ "New York", "New York" ],
    [ "North Carolina", "North Carolina" ],
    [ "North Dakota", "North Dakota" ],
    [ "Ohio", "Ohio" ],
    [ "Oklahoma", "Oklahoma" ],
    [ "Oregon", "Oregon" ],
    [ "Pennsylvania", "Pennsylvania" ],
    [ "Rhode Island", "Rhode Island" ],
    [ "South Carolina", "South Carolina" ],
    [ "South Dakota", "South Dakota" ],
    [ "Tennessee", "Tennessee" ],
    [ "Texas", "Texas" ],
    [ "Utah", "Utah" ],
    [ "Vermont", "Vermont" ],
    [ "Virginia", "Virginia" ],
    [ "Washington", "Washington" ],
    [ "West Virginia", "West Virginia" ],
    [ "Wisconsin", "Wisconsin" ],
    [ "Wyoming", "Wyoming" ]
  ]

發表在 Ruby on Rails | 留下評論