简体   繁体   English

用于条件URL的Javascript基于window.location.href附加或重定向

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

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). 我正在尝试制作一个书签,当点击它时将检查当前标签/窗口的URL,看它是否包含'char1'和/或'char2'(给定字符)。 If both chars are present it redirects to another URL, for the other two it will append the current URL respectively. 如果两个字符都存在,则重定向到另一个URL,对于另外两个字符,它将分别附加当前URL。

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. 我相信必须有一个更优雅的方式陈述这个比以下(迄今为止对我来说完美的工作)但我对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? 有没有更简单的方法来做到这一点,也许是对于vars或伪对象? Or better code? 还是更好的代码?

A (sort-of) refactoring of dthorpe's suggestion: 对dthorpe建议的一种(类型)重构:

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: 调用assign与为window.location分配值相同,无论如何你在方法中都使用了加法赋值+=运算符:

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. 这实际上会在调用assign方法之前将“append2”分配给window.location.href的末尾,从而使其成为冗余。

You could also reduce DOM lookups by setting window.location to a var. 您还可以通过将window.location设置为var来减少DOM查找。

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');
}

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;
 }
}

var location =window.location.href 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');}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM