-1
var SearchString = window.location.search.substring(1);
      var KeyValuePair = SearchString.split('=');

      for(var i = 0; i < KeyValuePair.length; i++){
         //seem not to be working
          var KeyValuePair = KeyValuePair[i].split(',');
          if(KeyValuePair[0] == VarSearch){
              return KeyValuePair[1];
          }

The example website address is: http://google.com/?type=talks. window.location.search retrieves "type=talks" and then split "=" away. The words will be "type, talks". Need to match "talks" with "talks" in json: type.

Is there a easy way to retrieve the last word after split '=' away?

Update The split word will be only ONE word. There is no "&" between the url address in. The retrieved word will be "type=talks" so need to take "talks" out from the retrieved word. "Talks" word will be compared with "talks" in json

joe
  • 1,115
  • 5
  • 21
  • 50

1 Answers1

1
var pairs = window.location.search.split("&");
var lastWord = pairs[pairs.lenght-1].split("=")[1];

UPDATE

If there is always only one pair then this may be simplified

var lastWord = window.location.search.split("=")[1];

But you'd better not to rely on a perfect address string, because everybody may type anything in the browser address bar

phts
  • 3,889
  • 1
  • 19
  • 31
  • no there is no "&" so url will show only "type=talks". all links will be only one word = like "type=entertainment", etc – joe Mar 27 '15 at 17:21
  • It will be OK, `pairs` array will contain only one element in this case. – phts Mar 27 '15 at 17:24
  • how come it shows one word correctly when there is no "&"? – joe Mar 27 '15 at 17:29