0

I have a code (listed below) that checks if your browser language is set to "de". If your browser is "de" it will send you to the adress "de.html". If it's not "de" you will be sent to "en.html". This works fine but here is the problem:

This is for a Webflow website and they have a editor witch is url/?edit. The code still try to check your language when going to that adress. It just prints out url/?editenenenenenenenenen.. and so on. Is there a way to check if the user is on url/?edit and if so, do nothing?

   var lang = window.navigator.language;
   var userLang = window.navigator.userLanguage;
    if (lang == "de" || userLang == "de") {
      window.location.href = window.location.href + "de.html";  
    }
    else {
      window.location.href = window.location.href + "en.html";
    }
Billy Berg
  • 49
  • 4

2 Answers2

0

If the URL is www.somedomain.com/editor?edit, then you can get the ?edit part using :

window.location.search // '?edit'

Using that :

if(window.location.search === '?edit') return;

or more loosely :

if(window.location.search.includes('?edit')) return;

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
0

Wrap another if statement around your current if like this:

if (!window.location.href.includes('/?edit')) { ... }

This will check if the user is on url/?edit and if so, does nothing.

As a whole:

var lang = window.navigator.language;
var userLang = window.navigator.userLanguage;
if (!window.location.href.includes('/?edit')) {
  if (lang == "de" || userLang == "de") {
    window.location.href = window.location.href + "de.html";
  } else {
    window.location.href = window.location.href + "en.html";
  }
}
CodeF0x
  • 2,624
  • 6
  • 17
  • 28
  • sigh.. apparently this doesn't work on IE.. any ideas? – Billy Berg May 25 '18 at 07:50
  • @BillyBerg What exactly doesn't work? Detecting if the user is on "/?edit" or changing the url? Are there errors in the dev console? – CodeF0x May 25 '18 at 07:59
  • Nether works, you get stuck on the blank page (the one with the code). – Billy Berg May 25 '18 at 08:02
  • @BillyBerg Hm, any errors in the developer console? – CodeF0x May 25 '18 at 08:03
  • Sorry.. don't have an console. This website is created in webflow and there is no console – Billy Berg May 25 '18 at 08:12
  • @BillyBerg Is there a way for you to test the actual site in an actual IE? Without detailed errors, it's very hard do debug this. I think that this post may help you: https://stackoverflow.com/questions/6297291/window-location-problem-in-ie?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – CodeF0x May 25 '18 at 08:22
  • hum.. I tried to switch window to document but nothing changes, still works in chrome but not IE – Billy Berg May 25 '18 at 08:50
  • @BillyBerg Maybe one of the other answers in the post I linked may help you. You could also try just to use `location`, e. g. `location = "yoururl";` – CodeF0x May 25 '18 at 08:55