我在另一篇文章中看到了一种非常手动的方法:How do I add a query parameter to a URL?
这似乎不是很直观,但有人提到了一种使用即将到来的"URL作用域“来实现这一点的更简单的方法。这个特性出来了吗,我该如何使用它呢?
发布于 2013-08-02 15:28:38
如果您使用的是stdlib mixer,您应该能够使用URL作用域,它提供了用于添加、查看、编辑和删除URL参数的辅助函数。下面是一个简单的例子:
$original_url = "http://cuteoverload.com/2013/08/01/buttless-monkey-jams?hi=there"
$new_url = url($original_url) {
log(param("hi"))
param("hello", "world")
remove_param("hi")
}
log($new_url)3H测试仪示例如下:http://tester.tritium.io/9fcda48fa81b6e0b8700ccdda9f85612a5d7442f
几乎忘记了,链接到docs:http://tritium.io/current (您需要单击URL类别)。
发布于 2013-08-02 07:25:31
AFAIK,没有这样做的内置方法。我将在这里发布我如何添加一个查询参数,确保它不会被复制,如果已经在url上:
在函数/main.ts文件中,您可以声明:
# Adds a query parameter to the URL string in scope.
# The parameter is added as the last parameter in
# the query string.
#
# Sample use:
# $("//a[@id='my_link]") {
# attribute("href") {
# value() {
# appendQueryParameter('MVWomen', '1')
# }
# }
# }
#
# That will add MVwomen=1 to the end of the query string,
# but before any hash arguments.
# It also takes care of deciding if a ? or a #
# should be used.
@func Text.appendQueryParameter(Text %param_name, Text %param_value) {
# this beautiful regex is divided in three parts:
# 1. Get anything until a ? or # is found (or we reach the end)
# 2. Get anything until a # is found (or we reach the end - can be empty)
# 3. Get the remainder (can be empty)
replace(/^([^#\?]*)(\?[^#]*)?(#.*)?$/) {
var('query_symbol', '?')
match(%2, /^\?/) {
$query_symbol = '&'
}
# first, it checks if the %param_name with this %param_value already exists
# if so, we don't do anything
match_not(%2, concat(%param_name, '=', %param_value)) {
# We concatenate the URL until ? or # (%1),
# then the query string (%2), which can be empty or not,
# then the query symbol (either ? or &),
# then the name of the parameter we are appending,
# then an equals sign,
# then the value of the parameter we are appending
# and finally the hash fragment, which can be empty or not
set(concat(%1, %2, $query_symbol, %param_name, '=', %param_value, %3))
}
}
}您想要的其他功能(删除、修改)也可以类似地实现(通过在functions/main.ts中创建一个函数并利用一些regex魔力)。
希望能有所帮助。
https://stackoverflow.com/questions/18002366
复制相似问题