I need a script that automatically adds to all pages on my website the ffv
query string parameter, as in the following examples:
mywebsite.com?ffv
mywebsite.com/page?ffv
mywebsite.com/basket?ffv
I need a script that automatically adds to all pages on my website the ffv
query string parameter, as in the following examples:
mywebsite.com?ffv
mywebsite.com/page?ffv
mywebsite.com/basket?ffv
The url is - https://mywebsite.com/path?ffv#hash
http[s]:// - protocol <br>
mywebsite.com - domain <br>
/path - path <br>
?ffv - query string <br>
\#value - hash
if you need to change query string, try JavaScript window.location
object
for change query:
function replace_search(value) {
"use strict";
if (typeof value === 'string' || typeof value === 'number') {
window.location.search = value;
}
}
on page side u cat use onclick event for manual change:
<button onclick="replace_search('query')">got to query</button>
Using the solutions presented here and here (with a slight adaptation), you can get the query string values and change them.
Starting from that, you can use the following code to do what you need:
//This allows you to read the query string
(function($) {
$.QueryString = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'))
})(jQuery);
(function(){
//This allows you to set the query string parameters
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + ( (value != '' ) ? "=" + value : "") + '$2');
}
else {
return uri + separator + key + ( (value != '' ) ? "=" + value : "");
}
}
// When you load the page, you check if the query string doesn't contain "ffv"
if(typeof $.QueryString["ffv"] == "undefined") {
//If it doesn't, you add it
window.location.href = updateQueryStringParameter(window.location.href,'ffv','');
}
})();
You need to check if ffv
is set before adding it, in order to avoid a loop.