Javascript检查url参数是否存在

btxsgosb  于 2023-01-04  发布在  Java
关注(0)|答案(3)|浏览(268)

我正在运行一个A/B测试,看看显示更多的项目是否更有利于转换。但似乎代码有时会导致错误。但我找不到任何错误,也不知道它们何时发生。
在我的测试中,我检查url参数IC是否存在,如果不存在,我将添加这个。
这是我的代码:

function checkIfAlreadyPaginated()
  {
        var field = 'IC';
    var url = window.location.href;
    if(url.indexOf('?' + field + '=') != -1)
        return true;
    else if(url.indexOf('&' + field + '=') != -1)
        return true;
    return false;
  }
function insertParam(key, value) {
        key = encodeURIComponent (key); value = encodeURIComponent (value);

        var kvp = document.location.search.substr(1).split('&');
        if (kvp == '') {
            return '?' + key + '=' + value;
        }
        else {

            var i = kvp.length; var x; while (i--) {
                x = kvp[i].split('=');

                if (x[0] == key) {
                    x[1] = value;
                    kvp[i] = x.join('=');
                    break;
                }
            }

            if (i < 0) { kvp[kvp.length] = [key, value].join('='); }

            return '?'+kvp.join('&');
        }
    }
var itemsPerPage = 48;
if(!checkIfAlreadyPaginated())
    {
      document.location.search = insertParam('IC', itemsPerPage);
    }

有人发现可能的问题吗?我正在通过VWO.com运行测试。

5t7ly7z5

5t7ly7z51#

如果有一个Javascript错误,你应该看到它在浏览器控制台,并与我们分享。
在任何情况下,我都会先创建一个JS对象,我发现这样更容易操作。
在下面的代码中,我添加了检查querystring的多个参数的选项。如果你只需要检查IC,你可以稍微简化它。我在一个空白的test.html上测试了它。

<script type="text/javascript">
// get the current params of the querystring
var querystringItems = document.location.search.substr(1).split('&');

// create an object
var querystringObject = {};
for(i=0;i<querystringItems.length;++i) {
    param = querystringItems[i].split('=');
    querystringObject[param[0]] = param[1];
}

// Define the keys to be searched for and their default value when they are not present
var requiredKeys = {"IC":48, "test": "me"};

// Do the checking on the querystringObject for each requiredKeys
var doreload = false;
for (var key in requiredKeys) {
    if (typeof querystringObject[key] == 'undefined') {
        doreload = true;
        // Create the missing parameter and assign the default value
        querystringObject[key] = requiredKeys[key];
    }
}

// If any of the requiredKeys was missing ...
if (doreload) {
    // rebuild the querystring
    var querystring = '?';
    for (var key in querystringObject) {
        querystring+=key+'='+querystringObject[key]+'&';
    }
    querystring=querystring.substr(0,querystring.length-1);
    // reload page
    document.location.search = querystring;
}

// assign the values to javascript variables (assuming you had it like this because you needed it)
var itemsPerPage = querystringObject.IC;
</script>
9njqaruj

9njqaruj2#

下面是一个示例来检查这一点:

//get URL params into string:
paramStr = window.location.substring(window.location.indexOf('?'), window.location.length;
//turn string into array
paramArray = paramStr.split('&');
//prepare final array of params
params = {};
//prepare the index of IC parameter
icLoc = -1; //this is negative 1 so that you know if it was found or not
//for each item in array
for(var i in paramArray){
    //push its name and value to the final array
    params.push(paramArray[i].split('='));
    //if the parameter name is IC, output its location in array
    if(params[i][0] === 'IC'){
        icLoc = i;
    }
}

如果未找到IC,则icLoc将为-1
如果找到,则URL参数中IC的值为params[icLoc][1]
查询字符串?foo=bar&code=cool&IC=HelloWorld的结果示例:

params = {'foo': 'bar', 'code': 'cool', 'IC': 'HelloWorld'}
icLoc = 2

查询字符串?foo=bar&code=cool的示例:

params = {'foo': 'bar', 'code': 'cool'}
icLoc = -1
vxf3dgd4

vxf3dgd43#

这里的id是我在测试中使用的参数,传递你想要检查它是否存在的参数。

function queryParamExistUrl(param = '') {
            if (new URLSearchParams(window.location.search).get(param) != null)
                return true
            return false
        }
        console.log(queryParamExistUrl('id'))

相关问题