如何从JavaScript中的URL获取特定项目?
我有一个URL,例如https://www.some.com/something-else/?utm_source=google-pagem&; utm_medium = paid& utm_campeign = sem-sem-sem-sem-something
。请求是提取UTM(Google Page,付费,SEM-SOMETHIG)的值以在POST请求中发送它们。实际上,我正在使用此JavaScript代码:
const utmSource = location.href.replaceAll('?', '$').
replaceAll('&', '$').
split('$').filter(utm => utm.includes('utm_source')).
map(utm => utm.split('=')).flat()[1];
对于每个UTM(UTM_Source,utm_campaign& utm_medium),但我认为这可能是一种更好的方法。
有更好的方法吗?
谢谢大家!
I have an URL like https://www.some.com/something-else/?utm_source=Google-PageM&utm_medium=Paid&utm_campaign=SEM-Something
. The request was to extract the values of the UTMs (Google-Page, Paid, SEM-Somethig) to send them in a post request. Actually I'm using this Javascript code:
const utmSource = location.href.replaceAll('?', '
For every UTM (utm_source, utm_campaign & utm_medium) but I think that maybe are a better way to do this.
Is there a better way to do it?
Thanks everyone!
).
replaceAll('&', '
For every UTM (utm_source, utm_campaign & utm_medium) but I think that maybe are a better way to do this.
Is there a better way to do it?
Thanks everyone!
).
split('
For every UTM (utm_source, utm_campaign & utm_medium) but I think that maybe are a better way to do this.
Is there a better way to do it?
Thanks everyone!
).filter(utm => utm.includes('utm_source')).
map(utm => utm.split('=')).flat()[1];
For every UTM (utm_source, utm_campaign & utm_medium) but I think that maybe are a better way to do this.
Is there a better way to do it?
Thanks everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何将其转换为URL并获取
searchParams
?要获取所有
searchParams
:获取
参数
以'utm_'开头:How about convert it to a URL and get
searchParams
?To get all
searchParams
:get
params
starts with 'utm_':有多种方法可以获取查询参数。然后我只需检查 utm_ 前缀,如下所示:
There are many ways to get the query parameters. Then I'd just check for the
utm_
prefix, something like this:您只需要参数值吗?我假设如果您打算在帖子中使用键和值,您就需要它们。
获取所有
usm_*
参数(带有键和值)的一种方法如下:然后,如果您只想获取键或值:
Do you only want the values of the parameters? I assume that you want both the key and the value if you attend to use them in a post.
One way to get all
usm_*
params (with key and values) is like this:Then, if you only want to get the keys or values: