Query String in JavaScript

前端 未结 3 1747
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 18:21

By using document.referrer we will get all the reference of URL in JavaScript, such as the following:

http://localhost/testwordpress/wp-admin/a         


        
相关标签:
3条回答
  • 2021-01-20 18:57

    To get the query string from document.referrer, you can use the split() method:

    var qs = document.referrer.split('?')[1];
    
    if (typeof qs !== 'undefined') {
        // qs contains the query string.
        // this would be "page=thesis-options&upgraded=true" in your case.
    }
    else {
        // there was no query string in document.referrer.
    }
    
    0 讨论(0)
  • 2021-01-20 19:03

    There are some functions around to do that. See this for example.

    0 讨论(0)
  • 2021-01-20 19:04

    If you are just looking to get the values from the query string I use the following function:

    function getQuerystring(key)
    {
      key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
      var qs = regex.exec(window.location.href);
      if(qs == null)
        return default_;
      else
        return qs[1];
    }
    

    Simply pass in the key you are looking for and get the value back. IE: getQueryString('upgraded') would return true

    0 讨论(0)
提交回复
热议问题