Global Directory
Global Directory
EXPLORE OUR SITES
London Stock Exchange Group
LSEG Data & Analytics
MyAccount
LSEG Perspectives
London Stock Exchange
FTSE Russell
LCH
Contact Us
Home
TR Internal
Cobalt javascript function to modify queryString param?
Patrick Schannach
Does Cobalt have a javascript function I can use to change the value of a query string parameter?
Find more posts tagged with
javascript
refinitiv-internal
Accepted answers
All comments
mark.maruska
I don't understand. Are you asking if there is a way to modify a query string parameter and then use that new url to reload the page (with the new url) so that other places in the JavaScript you will see the new query string parameter? ...because query string parameters are like input data; they're not stored or managed data that can be updated for all consumers. Please clarify.
Patrick Schannach
Yes, you are correct.
Example:
I want to change this:
http://www.mysite.com?param1=abc¶m2=def
to this:
http://www.mysite.com?param1=abc¶m2=xyz
Here is a link to some code I used in trial:
[
http://www.west-wind.com/weblog/posts/2009/Sep/07/Get-and-Set-Querystring-Values-in-JavaScript][1]
SetUrlEncodedKey: function (key, value, query) {
query = query || window.location.search;
var q = query + "&";
var re = new RegExp("[?|&]" + key + "=.*?&");
if (!re.test(q))
q += key + "=" + encodeURI(value);
else
q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&");
q = q.trimStart("&").trimEnd("&");
return q[0] == "?" ? q : q = "?" + q;
}
// some additions to String (used to encode queryString)
String.prototype.trimEnd = function (c) {
if (c)
return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
return this.replace(/\s+$/, '');
};
String.prototype.trimStart = function (c) {
if (c)
return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
return this.replace(/^\s+/, '');
};
String.prototype.escapeRegExp = function () {
return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
};
[1]:
http://www.west-wind.com/weblog/posts/2009/Sep/07/Get-and-Set-Querystring-Values-in-JavaScript
Ryan Morlok
I think Pat is asking for a helper function that manipulates strings interpreted as URLS. For example, if the constructor function was UrlHelper, he could do something like
var foo = new UrlHelper("
http://example.com?dog=cat
");
foo.setParam("dog", "woof");
foo.setParam("mouse", "cheese");
window.location = foo.getUrl(); //
http://example.com?dog=woof&mouse=cheese
shaggyalien
Cobalt.Url.js has a few functions to do this. Try using Chrome debugger's autocomplete, you'll be
able to see available functions inside Cobalt.Url.js
**Parsing a Url**
Cobalt.Url.Parse('
https://a.next.demo.westlaw.com/Search/Home.html?transitionType=Default&contextData=(sc.Default)')
**Result:**
{
"QueryString": {
"transitionType": "Default",
"contextData": "(sc.Default)"
},
"Protocol": "https",
"RootUrl": "
https://a.next.demo.westlaw.com
",
"Host": "a.next.demo.westlaw.com",
"Path": "/Search/Home.html"
}
**To do something like what ryan suggested.**
var urlComponents = Cobalt.Url.Parse('
https://a.next.demo.westlaw.com/Search/Home.html?transitionType=Default&contextData=(sc.Default)')
**You can Turn it back into a url like this.**
Cobalt.Url.Absolute(urlComponents);
**Output**
https://a.next.demo.westlaw.com/Search/Home.html?transitionType=Default&contextData=(sc.Default)
Quick Links
All Forums
Recent Questions
Terms of use
Privacy & Cookie Statement
Cookies settings
Do not sell my info
Whistleblowing
UK Bribery Act
Modern Slavery Act