Michael Schwartz
Michael Schwartz

Reputation: 8415

If hash contains only 1 string, ignore other if statements

So I'm working on updating one of my a coding playgrounds and am having a little trouble.

Basically, if the user ONLY has the "result" string in the hash like so...

testhash.html#d81441bc3488494beef1ff548bbff6c2?result

I ONLY want it to show the result ('only result was "result") and nothing else, but if the user has the editors in the string I want it to split and show the editors or in this example text...

testhash.html#d81441bc3488494beef1ff548bbff6c2?md,html,css,js,result

However, I'm having difficulty figuring out how to filter it where if it shows just the result string in the hash to ignore the other if statements.

How can I filter the function to where if it shows just the "result" string in the hash have it ignore the other if statements and only show result?

var url = window.location.hash,
    testhash = "#d81441bc3488494beef1ff548bbff6c2",
    arr = ["md", "html", "css", "js", "edit", "dark", "transparent"];

$(document).ready(function(e) {
  if (!url) {
      window.location.href = location.href + testhash + "?md,html,css,js,result";
  } else {
    // Show Editors If URL Contains Them
    if (url.indexOf("?") > -1) {
      if (url.indexOf("result") > -1) {
        if ($.inArray("result", arr) > -1) {
          $(document.body).append('only result was "result" 1');
          return false;
        }

        if (url.indexOf("md") > -1) {
          $(document.body).append('md');
        }
        if (url.indexOf("html") > -1) {
          $(document.body).append('html');
        }
        if (url.indexOf("css") > -1) {
          $(document.body).append('css');
        }
        if (url.indexOf("js") > -1) {
          $(document.body).append('js');
        }
        if (url.indexOf("dark") > -1) {
          $(document.body).append('dark');
        }
        if (url.indexOf("transparent") > -1) {
          $(document.body).append('transparent');
        }

        $(document.body).append('result');
      }
    }
  }
});

Upvotes: 0

Views: 61

Answers (1)

Mark
Mark

Reputation: 2063

I think what you want is not window.location.hash. rather, what you want is window.location.search

with given URL :

testhash.html#d81441bc3488494beef1ff548bbff6c2?md,html,css,js,result

When you use window.location.search, you would get ?md,html,css,js,result as result. Then it would be

var url = window.location.search;
$(document).ready(function(e) {
  if (!url) {
      //do your code
  } 
  else 
  {
     if (url.length > 1)
     {
         var search = url.substr(1); // remove char ?
         if (search == 'result')  // means that the parameter is ONLY result
         {
              $(document.body).append('only result was "result"');
              return false;
         }
         else //other strings WITH or WITHOUT the "result"
         {
              if (search.indexOf("result") > -1) 
              { $(document.body).append('result'); }

              if (search.indexOf("md") > -1) 
              { $(document.body).append('md'); }

               // and the rest
         }
      }
    }
});

demo : https://jsfiddle.net/jxom6zhp/1/

Upvotes: 1

Related Questions