How do I add a query parameter to a URL?

大城市里の小女人 提交于 2019-12-22 11:20:05

问题


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.


回答1:


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.

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!)




回答2:


I think there is going to be a new URL scope soon so you'll be able to do things like this much more easily!



来源:https://stackoverflow.com/questions/15712671/how-do-i-add-a-query-parameter-to-a-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!