Javascript 预选状态下拉框时获取JS错误

Javascript 预选状态下拉框时获取JS错误,javascript,jquery,Javascript,Jquery,我正在尝试自动选择状态下拉框的一个选项,其中包含url中的变量 出于某些原因,仅在Safari中,我得到此消息undefined不是对象求值“params.get”-不确定原因,它仅在Safari中发生。它在FF和铬合金中都能完美工作。你知道为什么吗?有没有办法改变代码,使其在所有浏览器中都能工作 jQuery(document).ready(function( $ ) { // Construct URL object using current browser URL var url

我正在尝试自动选择状态下拉框的一个选项,其中包含url中的变量

出于某些原因,仅在Safari中,我得到此消息undefined不是对象求值“params.get”-不确定原因,它仅在Safari中发生。它在FF和铬合金中都能完美工作。你知道为什么吗?有没有办法改变代码,使其在所有浏览器中都能工作

jQuery(document).ready(function( $ ) {

 // Construct URL object using current browser URL
  var url = new URL(document.location);

  // Get query parameters object
  var params = url.searchParams;

  // Get value of state
  var state = params.get("state");
   state = state.toUpperCase();
//alert(state);



$('#state option').each(function(){
    var $this = $(this); // cache this jQuery object to avoid overhead

    if ($this.val().toUpperCase() == state) { // if this option's value is equal to our value
        $this.prop('selected', true); // select this option
        return false; // break the loop, no need to look further
    }
});

});

如果我想从URL获取查询字符串,我将使用下面的代码片段,这非常简单

var queryStrings=window.location.href.slicewindow.location.href.indexOf'?'+1.split'&'; var pageParams=[]; 对于var i=0;i