請問想要合併兩個function成一個function,
並且function()裡可帶這兩個參數
請問要怎麼寫呢?
讓寫入和讀取的command指令可以寫在一個function裡頭
//寫入save字串到login.txt
function savewrite()
{
//write
var ReturnString = "";
var ipAddress = location.protocol+"//"+location.hostname+"/cgi-bin/";
var URLs = ipAddress + "PProduceXml.php";
var ss = "echo -n \"save"+"\" > /app/Web/cgi-bin/login.txt";
//alert("write:"+ss);
//var URLs = ipAddress + "Command.cgi";
$.ajax(
{
url: URLs,
data:"Action=TransmissionStart&submitString="+ss,
type:"GET",
async: false,
cache:false,
success: function(msg)
{
//alert("msg:"+msg);
},
error:function(xhr, ajaxOptions, thrownError)
{
//alert("xhr:"+xhr+" ajaxOptions:"+ajaxOptions+" thrownError:"+thrownError);
}
});
return ReturnString;
}
//讀取login.txt
function loginread()
{
var ReturnString = "";
var ipAddress = location.protocol+"//"+location.hostname+"/cgi-bin/";
var URLs = ipAddress + "PProduceXml.php";
var ss = "cat /app/Web/cgi-bin/login.txt";
$.ajax(
{
url: URLs,
data:"Action=TransmissionStart&submitString="+ss,
type:"GET",
async: false,
cache:false,
success: function(msg)
{
alert("msg:"+msg);
ReturnString = msg;
},
error:function(xhr, ajaxOptions, thrownError)
{
//alert("xhr:"+xhr+" ajaxOptions:"+ajaxOptions+" thrownError:"+thrownError);
}
});
return ReturnString;
}
參考看看吧!
function readOrWrite(isRead)
{
var ReturnString = "";
var ipAddress = location.protocol+"//"+location.hostname+"/cgi-bin/";
var URLs = ipAddress + "PProduceXml.php";
// 切換指令字串
var ss = (!!isRead) ? "cat /app/Web/cgi-bin/login.txt" : "echo -n \"save"+"\" > /app/Web/cgi-bin/login.txt";
$.ajax(
{
url: URLs,
data: "Action=TransmissionStart&submitString="+ss,
type: "GET",
async: false,
cache: false,
success: function(msg)
{
alert("msg:"+msg);
if(!!isRead) ReturnString = msg; // 輸入參數為trusy才assign msg給ReturnString
},
error:function(xhr, ajaxOptions, thrownError)
{
//alert("xhr:"+xhr+" ajaxOptions:"+ajaxOptions+" thrownError:"+thrownError);
}
});
return ReturnString;
}