Javascript detect language and redirect - javascript

I know there are many answers here. I have tried the following codes, but it keep not workings or keep redirect with showing a blank page.
I just want to detect all using Chinese language to go to the page with chinese, others go to English page.
How should I sueccessful do this function in a simple way?
Should i placed the detect code in both chi and eng page? and if the url included the "utm", how to keep this url with url after redirect?
http://www.testing.com/tc/testing.html?utm_source=test
<script type="text/javascript">
var userLang = navigator.language || navigator.userLanguage;
switch(userLang){
case 'zh-CN':
window.location.href = '/tc/testing.html;
break;
case 'zh-TW':
window.location.href = '/tc/testing.html';
break;
case 'zh-HK':
window.location.href = '/tc/testing.html';
break;
default:
// if it's something else fall back to the default
window.location.href = '/tc/testing.html';
break;
}
</script>
Or
<script type='text/javascript'>
var language = window.navigator.language;
if(language == 'zh-CN' || language == 'zh-TW' || language == 'zh-HK')
{
window.location.href = '../tc/testing.html'
}
else {
window.location.href = '../en/testing.html'
}
</script>

Related

Compare URL with string and path and insert link text

It is a post template that should work for different posts and in different languages. There should be no different templates for different languages. That's why I thought of the following. I compare the loaded url with string and path. If the condition is met, a URL and text will be added to the following link. Since I'm still at the beginning, I'm interested in your feedback and other approaches. Maybe you have any comments. So I can see what I can do better. Please help me so I can learn more. Thank you
var url = window.location.href
var path = window.location.pathname
var output1 = "http://localhost";
var output2 = output1 + path;
$(window).on('load', function () {
if (url == output1 + path) {
$("a.blog-wort").attr("href", "http://localhost/landing1/");
$('a.blog-wort').text('text 1');
} else if (url == output2) {
$("a.blog-wort").attr("href", "http://localhost/en/landing2/");
$('a.blog-wort').text('text 2');
} else if (url == output2) {
$("a.blog-wort").attr("href", "");
$('a.blog-wort').text('text 3');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="blog-wort"></a>

Redirects page base on user country (Javascript)

I’m struggling to find a good resource on how to execute this. I wanted to redirect the page base on user location. I know this required custom code but at the moment I’m stuck with it and most of the resources that I found are outdated. I’m using ipinfo.io to determine the user country.
Here’s the code that I’m using :
$.get(“https://ipinfo.io”, function(data) {
if(data.country !== “GB”){
console.log(“no gb”)
}else {
var test
test = ‘GB’
console.log(“THIS IS GB”)}
}, “jsonp”);
But then I’m stuck on how to use those value and redirect the page.
If anyone has any experience on this please guide me :pray:t3:
To redirect use location.href
$.get('https://ipinfo.io', function(data) {
if(data.country !== “GB”){
location.href = 'http://ADDRESS-TO-GB'
} else {
location.href = 'http://ADDRESS-TO-ANYWHERE'
}, 'jsonp');
$(function(){
$.get( "https://ipapi.co/json/", function( data ) {
switch(data.country){
case 'US':
window.location = 'http://www.google.us';
break;
case 'KR':
window.location = 'http://www.google.kr';
break;
default:
window.location = 'http://www.google.com';
break;
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
you can use a switch case to do redirection base on the callback received data.
try this one
$.getJSON('https://ipinfo.io', function(data) {
if(data.country == 'America'){
location = 'your_redirect_url';
return;
}else if(data.country == 'Beijing'){
location = 'your_redirect_url';
return;
}
})
you can use $.getJSON function then location.href to redirect.
$.getJSON( "https://ipapi.co/json/", function( data ) {
if(data.country !== "GB"){ location.href="your site url"}
if(data.country !== "US"){location.href="your site url"}
});

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;

Unescaped String in HTML

This Was sent to me in Facebook embedded in a HTML.. i didnt open this since i suspected it. Can anyone please tell me what it does?
Problem Statement : This is was written using document.write(unescape('<something here>');
I unescaped this.
`<script type="text/javascript"> // <![CDATA[
if ( (navigator.userAgent.indexOf('Android') != -1) ) {
document.location = "http://s3.amazonaws.com/video-asntjhwert/s.html";
} // ]]>
</script>
<script language=javascript>
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
location.replace("http://s3.amazonaws.com/video-asntjhwert/s.html");
}
</script>
<body>
<script>
if (navigator['userAgent']['indexOf']('Firefox') != -1) {
window['location'] = 'https://s3.amazonaws.com/video-asntjhwert/index.html';
} else {
if (navigator['userAgent']['indexOf']('Facebook Bot') != -1) {
window['location'] = 'http://google.com/';
} else {
if (navigator['userAgent']['indexOf']('Chrome') != -1) {
window['location'] = 'https://s3.amazonaws.com/video-asntjhwert/index.html';
} else {
window['location'] = 'http://s3.amazonaws.com/video-asntjhwert/s.html';
};
};
};
</script>
</body>'));
`
The first part checks which mobile OS You are using Android or iOS,
whereas the second part looks for PC browsers, and there is one thing common between them, its reroutes you to a webpage, hosted potentially on AWS servers and probably is a video, and you can conclude that its mostly an Ad! But be aware it may be a malware in disguise too injected by some one!

Javascript not redirecting

I have searched extensively for the javascript code to redirect to a different page on the same site and in the same folder and I have tried numerous things. I'm unable to get it to work in localhost or on the web-server. This is what I've tried:
<script type="text/javascript">
function confirm_submit()
{
var con = confirm("Submit form and print invoice?");
var url = 'dashboard.php?del=no';
if (con == true) {
document.location.href = url;
/*I have also tried:
window.location = 'dashboard.php?del=no';
window.location.replace("dashboard.php?del=no");
window.location.href = "dashboard.php?del=no";
*/
}
else {
return false;
}
}
</script>
This is the code on the button :
onclick="return(confirm_submit())"
From everything I've read this should work but it is not working. Can someone please tell me why it is not working and what I need to do to make it work? Thank you in advance.
EDIT: I have answered the question below. It's a workaround, but it is doing the job.
this will work without submitting your form
var url = window.location.href;
splitUrl = url.split("/");
splitUrl[splitUrl.length-1]="your page";
url = splitUrl.join("/");
window.location.href = url;
I have spent too much time on this issue so I have decided to go a different route. I'm using javascript to change the value of a hidden field and then using PHP to redirect to the other page as a result of the posted value of the hidden field. Thanks to all who have given their time to this matter on my behalf. Cheers
<script type="text/javascript">
function confirm_submit()
{
var con = confirm("Submit form and print invoice?");
if (con == true) {
document.getElementById('bool').value = true;
$("#edit_workorder").submit();
}
else {
return false;
}
}
</script>
And the PHP is:
if($bool == true) {
header("Location:dashboard.php?del=no");
exit;
}
It's doesn't really answer the question, but it works and that is more important to me right now.

Categories

Resources