背景:在項目開發的過程中總會遇到一些簡單但是也讓你頭疼不已的內容,現在總結一下,梳理一下思路,積累一下經驗。
1、JS打開新窗口
(1)超鏈接<ahref="http://www.jb51.net" title="腳本之家">Welcome</a>
等效於js代碼
window.location.href="http://www.jb51.net"; //在同當前窗口中打開窗口
(2)超鏈接<ahref="http://www.jb51.net" title="腳本之家"target="_blank">Welcome</a>
等效於js代碼
window.open("http://www.jb51.net"); //在另外新建窗口中打開窗口
2、截取URL中的某個字段
/* 提交表單 */
function SaveForm(){
var ctx=$("#ctx").attr("value");
var Request = new Object();
Request = GetRequest();
var pictorialId = Request['pictorialId'];//得到主頁面傳遞過來的pictorialId
var pictorialAId = Request['pictorialAId'];//得到主頁面傳遞過來的pictorialAId
alert(pictorialAId);
$.ajax({
//提交數據的類型 POST GET
type:"POST",
//提交的網址
url:ctx+"/sys/uploadPictorial/saveManuscript",
data:$('#sepManuscript').serialize(),// 你的formid
//返回數據的格式
datatype: "text",//"xml", "html", "script", "json", "jsonp", "text".
//成功返回之後調用的函數
success:function(data){
alert("稿件保存成功!");
/* window.location.href=ctx+"/sys/uploadPictorial/saveManuscript";*/
},
//調用出錯執行的函數
error:function(){
alert("稿件保存失敗!");
}
});
}
/* 獲取主頁面傳遞過來的pictorialId和 pictorialAId */
function GetRequest() {
var url = location.search; //獲取url中"?"符後的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}