javascritp 基礎語法收集

2013.12.04 慢慢收集中 , 不過 javascript 真的很少用到 ...

@彈出文字對話框
alert("your word");

@顯示文字在控制台
console.log("your word");

這個其實很好用 , 我都拿來 debug 用 ,例如chrome , 按下F12 , 就可以找到 Console

@動態寫入HTML
document.write("hello world");
但是只能醜醜的寫入純文字

進階用法:
html : <p id="inner"></p>
javascript: document.getElementById('inner').innerHTML = "Hello World!";

這種寫法彈性比較大 , 可以應用在不用的 HTML標籤
@網頁轉向

window.location.href='目標'
<input type="button" value="回首頁" onclick="window.location.href='.'" />

@文字分割
var t="this is test string";
var t1=t.split(" ",3);  //空格為分界 , 取前3個分割後的字串
t1 will be array => this,is,test
t1[0] =>this
t1[1] =>is

@function
建立:
function funtest(){
alert ("run function");}

呼叫:
funtest();