How to add parameters to a URL that already contains other parameters and maybe an anchor

后端 未结 9 643
逝去的感伤
逝去的感伤 2020-12-05 19:36

I\'m wondering how I can add a new parameter to an existing url. The problem is: the url may also contain an anchor.

For example:

http://www.example         


        
相关标签:
9条回答
  • 2020-12-05 20:23

    Something like this ?

    var param = "x=y";
    var split = url.split('#');
    url = split[0] + '&' + param + "#" + split[1];
    
    0 讨论(0)
  • 2020-12-05 20:26

    You can use this JS lib called URI.JS

    // mutating URLs
    URI("http://example.org/foo.html?hello=world")
        .username("rodneyrehm") 
            // -> http://rodneyrehm@example.org/foo.html?hello=world
        .username("") 
            // -> http://example.org/foo.html?hello=world
        .directory("bar")
            // -> http://example.org/bar/foo.html?hello=world
        .suffix("xml")    
            // -> http://example.org/bar/foo.xml?hello=world
        .hash("hackernews")
            // -> http://example.org/bar/foo.xml?hello=world#hackernews
        .fragment("")
            // -> http://example.org/bar/foo.xml?hello=world
        .search("") // alias of .query()
            // -> http://example.org/bar/foo.xml
        .tld("com")
            // -> http://example.com/bar/foo.xml
        .search({ foo: "bar", hello: ["world", "mars"] });
            // -> http://example.com/bar/foo.xml?foo=bar&hello=world&hello=mars
    

    or

    URI("?hello=world")
        .addSearch("hello", "mars")
            // -> ?hello=world&hello=mars
        .addSearch({ foo: ["bar", "baz"] })
            // -> ?hello=world&hello=mars&foo=bar&foo=baz
        .removeSearch("hello", "mars")
            // -> ?hello=world&foo=bar&foo=baz
        .removeSearch("foo")
            // -> ?hello=world
    
    0 讨论(0)
  • 2020-12-05 20:30

    I used parts of The Awesome One's solution, and a solution found on this question:

    Adding a parameter to the URL with JavaScript

    Combining them into this script:

    function addParameter(url, parameterName, parameterValue, atStart/*Add param before others*/){
        replaceDuplicates = true;
        if(url.indexOf('#') > 0){
            var cl = url.indexOf('#');
            urlhash = url.substring(url.indexOf('#'),url.length);
        } else {
            urlhash = '';
            cl = url.length;
        }
        sourceUrl = url.substring(0,cl);
    
        var urlParts = sourceUrl.split("?");
        var newQueryString = "";
    
        if (urlParts.length > 1)
        {
            var parameters = urlParts[1].split("&");
            for (var i=0; (i < parameters.length); i++)
            {
                var parameterParts = parameters[i].split("=");
                if (!(replaceDuplicates && parameterParts[0] == parameterName))
                {
                    if (newQueryString == "")
                        newQueryString = "?";
                    else
                        newQueryString += "&";
                    newQueryString += parameterParts[0] + "=" + (parameterParts[1]?parameterParts[1]:'');
                }
            }
        }
        if (newQueryString == "")
            newQueryString = "?";
    
        if(atStart){
            newQueryString = '?'+ parameterName + "=" + parameterValue + (newQueryString.length>1?'&'+newQueryString.substring(1):'');
        } else {
            if (newQueryString !== "" && newQueryString != '?')
                newQueryString += "&";
            newQueryString += parameterName + "=" + (parameterValue?parameterValue:'');
        }
        return urlParts[0] + newQueryString + urlhash;
    };
    

    Example: addParameter('http://www.example.com?foo=bar#hashme', 'bla', 'valuebla', false)

    Results in http://www.example.com?foo=bar&bla=valuebla#hashme

    0 讨论(0)
提交回复
热议问题