Javascript for conditional URL append or redirect based on window.location.href

自闭症网瘾萝莉.ら 提交于 2020-01-01 14:45:13

问题


I am trying to make a bookmarklet that when clicked will check the URL of the current tab/window to see if it contains 'char1' and/or 'char2' (a given character). If both chars are present it redirects to another URL, for the other two it will append the current URL respectively.

I believe there must be a more elegant way of stating this than the following (which has so far worked perfectly for me) but I don't have great knowledge of Javascript. My (unwieldy & repetitive) working code (apologies):

if (window.location.href.indexOf('char1') != -1 &&
    window.location.href.indexOf('char2') != -1)
{
    window.location="https://website.com/";
}
else if (window.location.href.indexOf('char1') != -1)
{
    window.location.assign(window.location.href += 'append1');
}
else if (window.location.href.indexOf('char2') != -1)
{
    window.location.assign(window.location.href += 'append2');
}

Does exactly what I need it to but, well... not very graceful to say the least.

Is there a simpler way to do this, perhaps with vars or a pseudo-object? Or better code?


回答1:


A (sort-of) refactoring of dthorpe's suggestion:

var hasC1  = window.location.href.indexOf('char1')!=-1
var hasC2  = window.location.href.indexOf('char2')!=-1
var newLoc = hasC1 
               ? hasC2 ? "https://website.com/" : window.location.href+'append1'
               : hasC2 ? window.location.href+'append1' : '';

if (newLoc)
    window.location = newLoc;

Calling assign is the same as assigning a value to window.location, you were doing both with the addition assignment += operator in the method anyway:

window.location.assign(window.location.href+='append2')

This would actually assign "append2" to the end of window.location.href before calling the assign method, making it redundant.

You could also reduce DOM lookups by setting window.location to a var.




回答2:


The only reduction I can see is to pull out the redundant indexof calls into vars and then test the vars. It's not going to make any appreciable difference in performance though.

var hasChar1 = window.location.href.indexOf('char1') != -1;
var hasChar2 = window.location.href.indexOf('char2') != -1;
if (hasChar1)
{
   if (hasChar2)
   {
      window.location="https://website.com/";
   }
   else
   {
      window.location.assign(window.location.href+='append1');
   }
} 
else if (hasChar2)
{
    window.location.assign(window.location.href+='append2');
}



回答3:


Kind of extendable code. Am i crazy?

var loc = window.location.href;
var arr = [{
  url: "https://website.com/",
  chars: ["char1", "char2"]
}, {
  url: loc + "append1",
  chars: ["char1"]
}, {
  url: loc + "append2",
  chars: ["char2"]
}];

function containsChars(str, chars)
{
  var contains = true;
  for(index in chars) {
    if(str.indexOf(chars[index]) == -1) {
      contains = false;
      break;
    }
  }
  return contains;
}

for(index in arr) {
 var item = arr[index];
 if(containsChars(loc, item.chars)) {
    window.location.href = item.url;
    break;
 }
}



回答4:


var location =window.location.href

if (location.indexOf('char1')!=-1 &&  location.indexOf('char2')!=-1)
{window.location="https://website.com/";} 
else if (location.href.indexOf('char1')!=-1) {window.location.assign(location+='append1');}
else if (location.indexOf('char2')!=-1) {window.location.assign(location+='append2');}


来源:https://stackoverflow.com/questions/2577480/javascript-for-conditional-url-append-or-redirect-based-on-window-location-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!