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

I'm 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" (this character). If both characters are present, they are redirected to a different URL, and for the other two it will add the current URL, respectively.

I believe there should be a more elegant way of saying this than the following (which has worked fine for me so far), but I'm not very good at Javascript. My (bulky and 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'); } 

Exactly what I need, but, well ... not very graceful, to say the least.

Is there an easier way to do this, perhaps with a vars or a pseudo object? Or is the code better?

+3
source share
4 answers

A (sorting) refactoring dthorpe clauses:

 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; 

The assign call is the same as the window.location value, you still executed the assignment operator += in the method:

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

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

You can also reduce the search for the DOM by setting window.location to var.

+2
source

The only shortcut I see is to pull out the excess call index in vars and then check the vars. However, this will not lead to noticeable differences in performance.

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

Type of extensible code. I've gone mad?

 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; } } 
+1
source

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

Source: https://habr.com/ru/post/913715/


All Articles