0

I have this JQuery/JS script that changes $_GET parameters of URL depending on which option from select element is selected. It works fine in Chrome but it won't change $_GET parameters or refresh in Safari 6.1.6.

 $("#promjena").change(function () {
    var v = $("#promjena").val();
    var href = window.location.href;

    if (v === "1") {
        if (href.includes("?akcija=kontrola")) {
            window.location.href = href.replace("?akcija=kontrola", "?akcija=default");
        } else if (href.includes("?akcija=signal")) {
            window.location.href = href.replace("?akcija=signal", "?akcija=default");
        } else {
            window.location.href = window.location.href + "?akcija=default";
        }
    }
    if (v === "2") {
        if (href.includes("?akcija=default")) {
            window.location.href = href.replace("?akcija=default", "?akcija=kontrola");
        } else if (href.includes("?akcija=signal")) {
            window.location.href = href.replace("?akcija=signal", "?akcija=kontrola");
        } else {
            window.location.href = window.location.href + "?akcija=kontrola";
        }
    }
    if (v === "3") {
        if (href.includes("?akcija=default")) {
            window.location.href = href.replace("?akcija=default", "?akcija=signal");
        } else if (href.includes("?akcija=kontrola")) {
            window.location.href = href.replace("?akcija=kontrola", "?akcija=signal");
        } else {
            window.location.href = window.location.href + "?akcija=signal";
        }
    }

});
Kreso
  • 25
  • 1
  • 8
  • Can you isolate the problem a little further by doing `console.log(v, href)` inside the change event handler and checking the values? Add it just before the `if` – sabithpocker Nov 14 '17 at 09:16
  • Console log returns: undefined undefined. – Kreso Nov 14 '17 at 09:21
  • You have to place `console.log` after `var v = $("#promjena").val();var href = window.location.href;` these two lines. Have you already placed it there? – sabithpocker Nov 14 '17 at 09:22
  • I'm sorry, i've placed it wrong. This is the return: 3http://localhost/papirus/kontrolna_lista/index.php?akcija=kontrola – Kreso Nov 14 '17 at 09:28
  • Can you try `var v = String($("#promjena").val());` or `var v = $("#promjena").val() + "";` – sabithpocker Nov 14 '17 at 09:46

2 Answers2

1

One chance is that val() is returning a number and you are expecting a string in if(v === "1") if thats an issue you can do:

 var v = $('#promjena').val() + ''; //to convert to string if not

Another chance is the support for includes in Safari < 9

If that is the case you can use a polyfill for includes as mentioned in the above page, or rewrite your code like:

if(href.indexOf("?akcija=default") !== -1)

Or use any other alternatives from this answer

sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • It gets in the if(v==="1") but it won't go further than `if (href.includes("?akcija=kontrola")) {`. I have ?akcija="kontrola" in the URL. – Kreso Nov 14 '17 at 09:56
  • use `if(href.indexOf("?akcija=default") !== -1)` instead of `includes` *(which is a newer addition to Javascript that is not supported in older browsers)* . But you can polyfill it if you want. – sabithpocker Nov 14 '17 at 09:58
0

try the following

     var v = $("#promjena").attr("value");

OR

     var v = parseInt($("#promjena").attr("value")).toString();

OR

    var v = parseInt($("#promjena").val()).toString();