Javascript - 为所有请求添加额外的全局参数

时间:2018-02-04 12:48:02

标签: javascript jquery event-handling

如何处理所有请求(提交,刷新页面......)并附加一个全局参数:

  

windowsID = XXXXXXX

我想将windowsID发送到服务器以识别当前窗口选项卡..

对于我使用的所有ajax:

private void btnUP_Click(object sender, EventArgs e)
{
    if (dgv != null)
    {
        if (dgv.Rows.Count > 0)
        {
            try
            {
                dgv.GridNavigator.SelectPreviousRow(1);
            }
            catch { }
        }
    }
}


private void btnDown_Click(object sender, EventArgs e)
{
    if (dgv != null)
    {
        if (dgv.Rows.Count > 0)
        {
            try
            {
                dgv.GridNavigator.SelectNextRow(1);
            }
            catch { }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

$('document').ready(function(){
    var updateQueryStringParam = function (key, value) {

    var baseUrl = [location.protocol, '//', location.host, location.pathname].join(''),
        urlQueryString = document.location.search,
        newParam = key + '=' + value,
        params = '?' + newParam;

    // If the "search" string exists, then build params from it
    if (urlQueryString) {
        var updateRegex = new RegExp('([\?&])' + key + '[^&]*');
        var removeRegex = new RegExp('([\?&])' + key + '=[^&;]+[&;]?');

        if( typeof value == 'undefined' || value == null || value == '' ) { // Remove param if value is empty
            params = urlQueryString.replace(removeRegex, "$1");
            params = params.replace( /[&;]$/, "" );

        } else if (urlQueryString.match(updateRegex) !== null) { // If param exists already, update it
            params = urlQueryString.replace(updateRegex, "$1" + newParam);

        } else { // Otherwise, add it to end of query string
            params = urlQueryString + '&' + newParam;
        }
    }

    // no parameter was set so we don't need the question mark
    params = params == '?' ? '' : params;

    window.history.replaceState({}, "", baseUrl + params);
};
updateQueryStringParam("wid","xxxxx");
console.log( window.location.href );  // whatever your current location href is

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

我已经为添加参数创建了此代码...但是网址可以为客户端用户编辑,并且可以将其删除为manualy