如何将查询参数添加到URL? [英] How do I add a query parameter to a URL?

查看:131
本文介绍了如何将查询参数添加到URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Tritium(Moovweb SDK)中向URL添加查询参数的最佳实践是什么?在不知道URL是否带有?"的地方寻找有用的东西和其他查询参数.

What's the best practice for adding a query parameter to a URL in Tritium (Moovweb SDK)? Looking for something that works where you don't know if the URL has a "?" and other query parameters already.

推荐答案

以下是Tritium的一小段内容,该内容应该可以帮助您进行Moovweb项目.只需将"query_param = true" 位替换为要添加的查询参数即可.

Here's a short snippet of Tritium that should help you out in your Moovweb project. Just replace the "query_param=true" bit with the query parameter you want to add.

它选择每个a标记的href,然后查找任何现有查询参数(通过在href中查找?").如果存在,则仅追加新的查询参数.如果href上没有现有的查询参数,则使用?.在网址中添加一个.

It selects the href of every a tag, then looks for any existing query parameters (by looking for a "?" in the href). If there are some existing, it just appends the new query parameter. If there are no existing query parameters on the href, it uses the ? to add one to the URL.

$q = "query_param=true"
$("//a[@href]") {
  %s = fetch("./@href")
  match(%s) {
    with(/\?/) {
      attribute("href", %s + "&" + $q)
    }
    else() {
      attribute("href", %s + "?" + $q)
    } 
  }
log(%s) 
}

(如果需要,您也可以将其转换为函数!)

(You could also turn that into a function if you wanted!)

这篇关于如何将查询参数添加到URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆