1

I have the code below. It has 3 dropdown option values. Currently, on change of dropdown selection an alert is thrown.

Now, I want to achieve the following: parameterise my dropdown values as URL's, so that i.e. when I enter the following URL in a browser: file://test.ds.waq.cb.uk/anywhere/UserData/PSStore02/u1718987/Desktop/new.html with ?mySelect=BMW at the end, then the browser opens the dropdown with the value BMW populated.

Or if I enter file://test.ds.waq.cb.uk/anywhere/UserData/PSStore02/u1718987/Desktop/new.html?mySelect=Audi, the browser opens the dropdown with Audi populated.

 <!DOCTYPE html>
    <html>
    <body>

    <p>Select a new car from the list.</p>

    <select id="mySelect" onchange="myFunction()">
      <option value="Audi">Audi
      <option value="BMW">BMW
      <option value="Volvo">Volvo
    </select>


    <p id="demo"></p>

Need to modify below script, to parameterise dropdown selection as URL, please advise.

<script>
function myFunction() {
  var x = document.getElementById("mySelect").value;
  alert (document.getElementById.innerHTML = "You selected: " + x);
}
</script>

</body>
</html>
user12403387
  • 174
  • 9

5 Answers5

1

You can easily fetch the url's parameter using expressions and some conditions to select the option. Change mySelect value in url to check it. JAVASCRIPT:

    function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
$(document).ready(function(){
    var data = getUrlVars()["mySelect"];
if(data == "Audi"){
  $('select').prop('selectedIndex', 0);
}
else if(data == "BMW"){
$('select').prop('selectedIndex', 1);
}
else if(data == "Volvo"){
$('select').prop('selectedIndex', 2);
}
});

HTML:

<select>
  <option class="Audi">Audi</option>
  <option class="BMW">BMW</option>
  <option class="Volvo">Volvo</option>
</select>
Ahmed Ali
  • 1,908
  • 14
  • 28
0

You can read url params once select box loads, see below code

HTML:

    <!DOCTYPE html>
        <html>
        <body>

        <p>Select a new car from the list.</p>

        <select id="mySelect" onchange="myFunction()" onload="setMySelect()">
          <option value="Audi">Audi
          <option value="BMW">BMW
          <option value="Volvo">Volvo
        </select>


        <p id="demo"></p>


<script>
 function setMySelect() {
    var urlParams = new URLSearchParams(window.location.search);
    var mySelect = urlParams.get('mySelect');
    if(mySelect)
    document.getElementById("mySelect").value = mySelect;
}
function myFunction() {
  var x = document.getElementById("mySelect").value;
  alert (document.getElementById.innerHTML = "You selected: " + x);
}
</script>

</body>
</html>
David Thomas
  • 249,100
  • 51
  • 377
  • 410
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
  • this does not seem to work, its not even executing the function – user12403387 Nov 20 '19 at 13:18
  • @Bhushan Kawadkar this does not seem to work please advise – user12403387 Nov 20 '19 at 13:23
  • Basically when I open FireFox, and enter the url file://test.ds.waq.cb.uk/anywhere/UserData/PSStore02/u1718987/Desktop/new.html with ?mySelect=BMW BMW is not selected on the dropdown it still shows Audi, also if I enter file://test.ds.waq.cb.uk/anywhere/UserData/PSStore02/u1718987/Desktop/new.html with ?mySelect=Volvo still Audi is shown – user12403387 Nov 20 '19 at 13:37
  • When you enter the url on browser and hit ENTER button then url stays same or it get changed after page load? If url is getting changed after page load then this solution will not work, you need to make changes on server end to preserve the url or read request param with backend code and pass it to page so that page can read the params and set select box values accordingly – Bhushan Kawadkar Nov 20 '19 at 13:46
0

use this answer to extract search queries. Then just set value on load with the proper value.

<script>
function setValue(value) {
  document.getElementById("mySelect").value = value;
  alert (document.getElementById.innerHTML = "You selected: " + x);
}
</script>

UPDATE to have both in the same script.

<script>
function setValue(value) {
  document.getElementById("mySelect").value = value;
  alert (document.getElementById.innerHTML = "You selected: " + x);
}
let urlParams = new URLSearchParams(window.location.search);
setValue(urlParams.get('mySelect');
</script>
alireza
  • 518
  • 5
  • 15
0

You could try the following.

var url = new URL(window.location);
var params = new URLSearchParams(url.search.slice(1));
for (let [key, value] of params.entries()) {
    if(params.has(key) && key == "mySelect") {
        document.getElementById(key).value = value;
    }
}
document.getElementById("mySelect").addEventListener("change", (e) => {
    params.set("mySelect",e.target.value);
    url.search = params.toString();
    let new_url = url.toString();
    window.location.assign(new_url);
})
0

I needed the following window.onload as this function was not getting loaded.

<!DOCTYPE html>
            <html>
            <body>

            <p>Select a new car from the list.</p>

            <select id="mySelect" onchange="myFunction()" onload="setMySelect()">
              <option value="Audi">Audi</option>
              <option value="BMW">BMW</option>
              <option value="Volvo">Volvo</option>
            </select>


            <p id="demo"></p>


    <script>
     <!-- function setMySelect() { -->

     window.onload = function() {
        var urlParams = new URLSearchParams(window.location.search);
        var mySelect = urlParams.get('mySelect');
        if(mySelect)
        document.getElementById("mySelect").value = mySelect;
    }
    function myFunction() {
      var x = document.getElementById("mySelect").value;
      alert (document.getElementById.innerHTML = "You selected: " + x);
    }
    </script>

    </body>
    </html>
user12403387
  • 174
  • 9