javascript - How to get parameter name? -


this question has answer here:

i have url that:

//my-url-adresses.com/web/assets/js/javascripts?v=5.2&marker=2017-05/logo_30dkmohpnlq8cfioyfqhmarkerv2.png&cdn=//cdn.mywebsite.com/files/

and want part: (after cdn=):

//cdn.mywebsite.com/files/

i have provided code below. please correct wrong. in advance.

var scripts = document.getelementsbytagname('script');  var index = scripts.length - 1;  var myscript = scripts[index];  var scripturl = myscript.src;  var cdnurl = getparameterbyname("cdn", scripturl);  var markerurl = getparameterbyname("marker", scripturl);      function getparameterbyname(name, url) {      if (!url) url = window.location.href;      name = name.replace(/[\[\]]/g, "\\$&");      var regex = new regexp("[?&]" + name + "(=([^&#]*)|&|#|$)"),          results = regex.exec(url);      if (!results) return null;      if (!results[2]) return '';      return decodeuricomponent(results[2].replace(/\+/g, " "));  }      console.log(markerurl);
<script id="scripts" src="//my-url-adresses.com/web/assets/js/javascripts?v=5.2&amp;marker=2017-05/logo_30dkmohpnlq8cfioyfqhmarkerv2.png&amp;cdn=//cdn.mywebsite.com.com/files/" defer></script>

i noticed code not able access script url using index property. noticed have id in script tag. better use that, otherwise if have script tag function in page @ bottom of page, those(since finding last script tag in page) , since don't have src won't fetch result.

here solution using getelementbyid()

var myscript = document.getelementbyid('scripts');  var scripturl = myscript.src;  var cdnurl = getparameterbyname("cdn", scripturl);  var markerurl = getparameterbyname("marker", scripturl);      function getparameterbyname(name, url) {      url = decodeuricomponent(url);      name = name.replace(/[\[\]]/g, "\\$&");      var regex = new regexp("[?&]" + name + "(=([^&#]*)|&|#|$)"),          results = regex.exec(url);      if (!results) return null;      if (!results[2]) return '';      return decodeuricomponent(results[2].replace(/\+/g, " "));  }      console.log(markerurl);
<script id="scripts" src="//my-url-adresses.com/web/assets/js/javascripts?v=5.2&amp;marker=2017-05/logo_30dkmohpnlq8cfioyfqhmarkerv2.png&amp;cdn=//cdn.anitur.com/web/" defer></script>


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -