Javascript add multiple if - window.location.hostname - javascript

I have 5 websites using the same DB, and I need to add an global .js to check window.location.hostname and redirect if dont match one of my domains:
if (window.location.hostname != 'mysite1.com')
if (window.location.hostname != 'mysite2.com')
if (window.location.hostname != 'mysite3.com')
if (window.location.hostname != 'mysite4.com')
if (window.location.hostname != 'mysite5.com')
{
window.location.href = 'https://google.com';
}
The js will be inserted in the DB, and will display in all my sites.
HOW to add multiple instances of IF...window.location.hostname ... in the RIGHT WAY

I don't know if I understood your question. If you want to merge those 'if' conditions you can do like this:
if(!['mysite1.com','mysite2.com','mysite3.com','mysite4.com','mysite5.com'].includes(window.location.hostname)){
window.location.href = 'https://google.com';
}

Change your code and add AND as follows:
if (window.location.hostname != 'mysite1.com'
&& window.location.hostname != 'mysite2.com'
&& window.location.hostname != 'mysite3.com'
&& window.location.hostname != 'mysite4.com'
&& window.location.hostname != 'mysite5.com')
{
window.location.href = 'https://google.com';
}

I would define a list (array) of all domains first and then check if the current domain is on the list
listOfHostnames = ['mysite1.com', 'mysite2.com', 'mysite3.com'];
if (!~window.location.hostname.indexOf(listOfHostnames)) {
window.location.href = 'https://google.com';
}

Related

My redirect isn't working well with IPInfo.io

I need help with redirect to subdomains. With IPinfo I get country of visitor then i redirect him to one of my subdomains and i need this look like ua.mysite or kz.mysite or ru.mysite
I tried window.location.href = data.country + 'mysite' but this didn't work. Hope you can help me! :)
// Let's check if we have the value in localstorage
if (localStorage.getItem('country') == 'RU') {
// Already have the value in localStorage no need to make call to IPinfo
window.location.hostname = 'ru.mysite.tech'
} else if(localStorage.getItem('country') == 'KZ') {
// Already have the value in localStorage no need to make call to IPinfo
window.location.hostname = 'kz.mysite.tech'
} else if(localStorage.getItem('country') == 'UA') {
// Already have the value in localStorage no need to make call to IPinfo
window.location.hostname = 'ua.mysite.tech'
}
else{
// No cached data, let's get it from IPinfo
fetch('https://ipinfo.io/json?<MyToken>')
.then(res => res.json())
.then(data => {
if(data.country == 'UA'){
localStorage.setItem('ipinfo', data.country)
window.location.hostname = 'ua.mysite.tech'
}
else if(data.country == 'KZ'){
localStorage.setItem('ipinfo', data.country)
window.location.hostname = 'kz.mysite.tech'
}
else {
localStorage.setItem('ipinfo', data.country)
window.location.hostname = 'ru.mysite.tech'
}
})
}
}```
Updates to window.location.hostname don't cause redirect.
You should update window.location directly like this (note the "https:/"):
if (localStorage.getItem('country') == 'RU') {
// Already have the value in localStorage no need to make call to IPinfo
window.location = 'https://ru.mysite.tech'
}

How can ı do if is true when my url address

Hello everyone ı want to control my url address in my code ı want to redirect when url its true
if (data.is_okey && **URL** == true) {
window.location.replace('https://google.com.tr');
}
The current address in your browser is available with the variable window.location.href
So you should write:
if (data.is_okey && window.location.href == "**SOME URL YOU'D LIKE TO BE TRUE**") {
window.location.replace('https://google.com.tr');
}
I see that you want to check if a url is equal to a unique/decided one?
For example;
let checkUrl = "/login"
let url = window.location.pathname;
if(data.is_okey && url != checkUrl) {
window.location.replace("/login")
}
If you want sth like this. Please try it or explain with more details.

Check browser and language

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

How to redirect to the base URL if the given URL ends with a slash?

I have this link http://localhost:3007/sellers-toolkit/, but the right page is only the one without a trailing slash. So I would like a redirection to http://localhost:3007/sellers-toolkit.
I tried to write this part in JavaScript, but it just makes the page reload and nothing happens. What is the right way to achieve this behavior?
var url = "http://localhost:3007/sellers-toolkit/";
if (url) {
url = "http://localhost:3007/sellers-toolkit";
window.location.href = url;
}
my solution for this issue is that i was trying to do that on the javascript side but i found that i have to do this on my node server side.
i used this code:
if (req.url == '/sellers-toolkit' || req.url == '/sellers-toolkit/' ) {
category = 'Selling Privately';
}
Try like below:
var url = "http://localhost:3007/sellers-toolkit/";
var spe_char_last =url.substr(url.length - 1);
if(spe_char_last == '/')
{
url = url.slice(0, -1);
if (url) {
url = "http://localhost:3007/sellers-toolkit";
window.location.href = url;
}
}

If statement does nothing javascript

Okay so here's a simple question that I understood but got asked... let's say you have an if statement
if (window.location.href == 'http://' ||
window.location.href == 'http://?pg=pgOldMainMenu' ||
window.location.href == 'http://default.asp#')
else {}
How can you say "if positive do nothing, else load this HTML"?
Simply like this
var href = window.location.href;
if (href == 'http://' ||
href == 'http://?pg=pgOldMainMenu' ||
href == 'http://default.asp#') {} // do nothing
else {
href = 'url you want to load';
}

Categories

Resources