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?
source share