我可以使用
GetQueryParam
函数从 WebLib.WebTools
单元中的 answer获取参数,以从当前 URL 获取参数。
人们会认为与获取参数相反的是使用
SetQueryParam
设置参数,但事实并非如此! SetQueryParam
不存在。
如果我运行以下代码:
SetQueryParam('first_name', 'Shaun');
SetQueryParam('last_name', 'Roselt');
然后我收到一条错误消息
找不到标识符“SetQueryParam”
使用Delphi代码设置查询参数的函数和单位是什么?
一些补充信息。这是我用来更新 URL 参数的当前代码:
asm
function SetQueryParam(key, value, url) {
if (!url) url = window.location.href;
let re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),hash;
if (re.test(url)) {
if (typeof value !== 'undefined' && value !== null)
url = url.replace(re, '$1' + key + "=" + value + '$2$3');
else {
hash = url.split('#');
url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
}
}
else {
if (typeof value !== 'undefined' && value !== null) {
const separator = url.indexOf('?') !== -1 ? '&' : '?';
hash = url.split('#');
url = hash[0] + separator + key + '=' + value;
if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
}
}
window.history.pushState({ path: url }, '', url);
}
SetQueryParam('first_name', 'Shaun');
SetQueryParam('last_name', 'Shaun');
end;
上面的代码可以工作并且满足我的需要,但它是 JavaScript 和 Delphi 代码的混合。这太乱了,我不喜欢它。
当然,必须有一个内置的 Delphi 方法来在 TMS Web Core 中设置 URL 参数?
TMS Web Core 具有三个全局变量,您可以使用它们来访问 JavaScript 中
document
、window
和 console
对象的常用功能。
这就是它们在 Web 单元中声明为全局变量的方式:
var
document: TJSDocument;
window: TJSWindow;
console: TJSConsole;
从全局 window 变量中,您可以像在 JavaScript 代码中使用它一样调用 PushState。
通过访问
window
对象并使用它的函数,您可以将 JavaScript 代码重写为本机 Delphi 代码。据我所知,TMS Web Core 中没有内置函数可以与 uris 一起使用。