首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有简单的方法来添加/删除/修改Tritium中的URL的查询参数?

有没有简单的方法来添加/删除/修改Tritium中的URL的查询参数?
EN

Stack Overflow用户
提问于 2013-08-02 03:00:37
回答 2查看 272关注 0票数 3

我在另一篇文章中看到了一种非常手动的方法:How do I add a query parameter to a URL?

这似乎不是很直观,但有人提到了一种使用即将到来的"URL作用域“来实现这一点的更简单的方法。这个特性出来了吗,我该如何使用它呢?

EN

回答 2

Stack Overflow用户

发布于 2013-08-02 15:28:38

如果您使用的是stdlib mixer,您应该能够使用URL作用域,它提供了用于添加、查看、编辑和删除URL参数的辅助函数。下面是一个简单的例子:

代码语言:javascript
复制
$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类别)。

票数 2
EN

Stack Overflow用户

发布于 2013-08-02 07:25:31

AFAIK,没有这样做的内置方法。我将在这里发布我如何添加一个查询参数,确保它不会被复制,如果已经在url上:

在函数/main.ts文件中,您可以声明:

代码语言:javascript
复制
# 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魔力)。

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18002366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档