-2

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
clami219
  • 2,958
  • 1
  • 31
  • 45
gdjc
  • 45
  • 1
  • 10
  • When you say 'all urls' do you mean those in `a` elements only, or is this including the `action` attribute on `form` elements, and `background-images` etc? – Rory McCrossan May 26 '15 at 08:58
  • I mean physical pages, not page elements – gdjc May 26 '15 at 09:02
  • @gdjc To make it very simple: you mean the urls in the url bar in the browser, right? You want them to have automatically "?ffv" attached, right? – clami219 May 26 '15 at 09:10
  • @clami219 - Yes, all URLs in the browser bar to have the aforementioned string attached to them. – gdjc May 26 '15 at 09:23

2 Answers2

0

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>
Alex Kashin
  • 575
  • 6
  • 13
  • That puts the page in to an eternal looping refresh though? – gdjc May 26 '15 at 09:14
  • create function for change and execute it if u need – Alex Kashin May 26 '15 at 09:18
  • Nope not working. Another option is to change the URL: At present it has: /p/ Change it to /product/ I'm trying to drop a cookie so that I can run a fairly large A/B test (the _ff=v is the cookie) – gdjc May 26 '15 at 09:54
  • /p/ - its pathname attr, if u wanna change it - use window.location.pathname – Alex Kashin May 26 '15 at 10:05
  • Have you got a code example? Am not exactly a master in jQuery or JavaScript – gdjc May 26 '15 at 10:51
  • function replace_path(value) { "use strict"; if (typeof value === 'string' || typeof value === 'number') { window.location.path = value; } } – Alex Kashin May 26 '15 at 10:54
0

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.

Community
  • 1
  • 1
clami219
  • 2,958
  • 1
  • 31
  • 45