How to get current recordId from the url

The code below helps to get url params from javascript code

        const recordId = getUrlParam('recordId');
        console.log(recordId);

        function getUrlParam(name) {
            const url = new URL(window.location.href);
            let param;
            for (var key of url.searchParams.keys()) {
                if (key.toLowerCase() === name.toLowerCase()) {
                    param = url.searchParams.get(name);
                    break;
                }
            }

            if (!param && name.toLowerCase() === 'recordid') {
                param = getRecordIdFromPath();
            }
            return param;
        }

        function getRecordIdFromPath() {
            let pathName = window.location.pathname;
            if (pathName.indexOf('/r/rec') !== -1) {
                pathName = pathName.substr(pathName.indexOf('/r/rec') + 3);
                if (pathName.indexOf("/") !== -1) {
                    pathName = pathName(0, pathName.indexOf('/'))
                }
                return pathName;
            }
            return undefined;
        }
1 Like