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;
        }
2 Likes

Does it works with “open modal” features from a list details block ? Thanks
UPDATE : works well :white_check_mark:

Hey, I want to use the code to enable my list in softr to use the contents of the current pages recordId.
Where do I have to paste the code?

Hey @LuisaMarie,

You will need to add this to Pages (your page) > page settings > header area.

It looks like this what is wrong?

I write script befor and after the code, now I cant’t see him anymore, but it still doesn’t work :frowning:

Hi Arthur and the rest,

I have a practical issue with this code:

Basically I have a menucard from which users can book ITEM_ID to their PROJECT_ID. To be able to have Softr send these two identifiers, I have a button on a menucard open a ITEM-list-details-block (so I can use the custom code above to send the ITEM_ID based on the URL) and a PROJECT-list-block with a button from which I can send the PROJECT_ID (and added to that the ITEM_ID).

The problem that I experience is that Softr is lagging in extracting the ITEM_ID from the URL - so much that if you press the button too fast it results in an error.

Can anyone help me to have the list-block (which contains the button) to either (1) delay before its shown by 2 seconds or (2) to be shown after Softr extracted the ITEM_ID from the URL ?