js筆記

常用軟體,程式語言的筆記和疑問
Forum rules
盡量做到:
每一個貼子都精心編輯;
每一個疑問都得到解答。
ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

js筆記

Post by ejsoon » 2016 Apr 05, 22:22

純js添加元素

Code: Select all

var the_a = document.createElement("a");
the_text = document.createTextNode("text");
the_a.href = "link";
the_a.onclick = function(){
your programme;
};
the_a.className = "a_classname";

the_a.appendChild(the_text);
the_div.appendChild(the_a);

ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

跳轉手機端

Post by ejsoon » 2016 Apr 07, 16:55

Code: Select all

<script language="javascript"> 
function is_mobile() { 
var regex_match = /(nokia|iphone|ipad|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i; 
var u = navigator.userAgent; 
if (null == u) { 
return true; 
} 
var result = regex_match.exec(u); 
if (null == result) { 
return false 
} else { 
return true 
} 
} 
if (is_mobile()) { 
document.location.href= 'YOUR WAP SITE'; 
} 
</script>

ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

HTML自動跳轉

Post by ejsoon » 2016 Apr 08, 13:12

<meta http-equiv="refresh" content="0.2;url=http://www.kaigesheji.com/wangzhanjianshe/">

ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

ajax-post

Post by ejsoon » 2016 Apr 28, 08:33

Code: Select all

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        alert(xmlhttp.responseText);
    }
}
xmlhttp.open("POST","target.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");              //or multipart/form-data
xmlhttp.send("one=two&two=three");
如果要改爲js post application/json:
https://stackoverflow.com/questions/125 ... son-format
sending-a-json-to-server-and-retrieving-a-json-in-return-without-jquery

如果要封裝成ajaxxhr.js:

Code: Select all

function xhr_get(url, cFun){
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function() {
			if (this.readyState == 4 && this.status == 200) {
				cFun(this);
			}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send();
}

function xhr_post(url, param, cFun, header){
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function() {
			if (this.readyState == 4 && this.status == 200) {
				cFun(this);
			}
	}
	xmlhttp.open("POST", url, true);

	var header_str = header == "json" ? "application/json" : "application/x-www-form-urlencoded";
	xmlhttp.setRequestHeader("Content-type", header_str);
	xmlhttp.send(param);
}
如果要拿到form數據(來自這裡)(改進在):

Code: Select all

var formElements=document.getElementById("myForm").elements;    
var postData={};
for (var i=0; i<formElements.length; i++)
    if (formElements[i].type!="submit")//we dont want to include the submit-buttom
        postData[formElements[i].name]=formElements[i].value;

ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

Re: ajax-post

Post by ejsoon » 2016 Apr 28, 08:34


ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

js點擊返回

Post by ejsoon » 2016 May 19, 18:09

onclick="javascript:history.back();"

ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

js頁面跳轉的幾種方式

Post by ejsoon » 2016 May 20, 14:17

window.location.href="your_site";
self.location="your_site";
window.navigate("your_site");
top.location="your_site";

參考自:http://www.jb51.net/article/25403.htm

js 刷新頁面(來自這裡):
1,history.go(0)
2,location.reload()
3,location=location
4,location.assign(location)
5,document.execCommand('Refresh')
6,window.navigate(location)
7,location.replace(location)
8,document.URL=location.href
自動刷新:
<meta http-equiv="refresh" content="20">
自動跳轉:
<meta http-equiv="refresh" content="20;url=http://www.jb51.net">
自動刷新js版:
<script language="JavaScript">
function myrefresh()
{
window.location.reload();
}
setTimeout('myrefresh()',1000);
</script>

ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

跳轉手機端 之二

Post by ejsoon » 2016 May 26, 15:58

Code: Select all

        var sUserAgent = navigator.userAgent.toLowerCase();
        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
        var bIsMidp = sUserAgent.match(/midp/i) == "midp";
        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
        var bIsAndroid = sUserAgent.match(/android/i) == "android";
        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

        if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
            window.location.href = 'm/index.html';
        }

ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

js timing events

Post by ejsoon » 2016 Jun 26, 21:52

myVar1 = setInterval(myTimer, 3000);
myVar2 = setTimeout(myTimer, 3000);
myVar3 = setInterval(function(){ myTimer() }, 3000);
clearTimeout(myVar);
clearInterval(myVar)

ejsoon
Site Admin
Posts: 3767
Joined: 2016 Jan 10, 22:15

console log

Post by ejsoon » 2016 Jul 01, 10:28

console.log(new_array);

Post Reply