Dual language browsers redirect Javascript - javascript

I am trying to redirect English browsers to xyz.com/?lang=en while letting Swedish ones stay on xyz.com
I have been trying :
var type=navigator.appName
if (type=="Netscape")
var lang = navigator.language
else
var lang = navigator.userLanguage
//cut down to first 2 chars of country code
var lang = lang.substr(0,2)
// Swedish
if (lang == "sv")
window.location.replace('????')
// if none of above (default to English or any other)
else
window.location.replace('xyz.com/?lang=en')
</script>
But I don't know how to write the Swedish URL since it's not a redirection as the default language is swedish... writing the xyz.com gets me into a redirection loop
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langlight">Svenska</div>';
}
else
{
echo '<div class="langbold">Svenska</div>';
}
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langbold">English</div>';
}
else
{
echo '<div class="langlight">English</div>';
}
enter code here

if (lang !== "sv") {
window.location.replace(window.location.href + '?lang=en');
}

You are always checking the language from the navigator.
This way everytime the page loads it will try to reload the page with the altered or not url.
You need to place a condition where it will not reload the page..
That should be when there is a URL parameter passed.
So check for the querystring as well and override the default if a value exists in the url.
<script type="text/javascript">
(function(undefined){
var queryString = {},
search = window.location.search.substring(1), // read the current querystring
searchItems = search.length?search.split('&'):[]; // and split it at the different params if any exist
for(var i = 0, len = searchItems.length; i < len; i++){ // for each parameter passed
var parts = searchItems[i].split('='); // split the key/value pair
queryString[parts[0].toLowerCase()] = parts[1]; // and store it to our queryString variable
}
if (queryString.lang === undefined){ // if there is no lang parameter passed do your checking otherwise skip this step completely and use the url lang parameter.
var type=navigator.appName, lang = '';
if (type=="Netscape") {
lang = navigator.language.substr(0,2);
} else {
lang = navigator.userLanguage.substr(0,2);
}
if (lang != "sv"){
if (searchItems.length){
window.location.replace(window.location.href + '&lang=en');
} else {
window.location.replace(window.location.href + '?lang=en');
}
}
}
}());
</script>
Notice: Although, as #missingno mentions in his comment to your question, this is best handled server-side than client-side.

Related

How to show dialogue if url contains something

If my URL is any of these type:
domain.com/page.html?refer=whatsapp
domain.com/page.html#whatsapp
Then I want to show a dialogue that shows "You have been referred from Whatsapp".
And if the url is accessed without the whatsapp part, then it must not show the dialoge box.
What I have tried is:
<script type="text/javascript">
var url = window.location.href;
if (url.search("#wa") >= 0) {
//found it, now do something
}
alert("Good Afternoon");
} else {
null //if not just do nothing
}
</script>
But no luck.
It helps you
var url = window.location.href;
// For example. In real app remove it
url = 'http://website.com/page.html#whatsapp';
// Search our #ID
var source = url.substring(url.lastIndexOf('#') + 1);
// For salutation text
var salutation = null;
// Iterate variants
switch ( source ) {
case 'whatsapp' : salutation = 'You have been referred from Whatsapp'; break;
default : salutation = 'unknown website';
}
alert ( salutation );

Javascript Cookie Not Setting

I have an application that needs to pop a URL based on a Query String sent to it. Unfortunately, we can't insert any javascript into the application itself, but we can insert an iFrame that loads a page running javascript. There is a bug in the application where it loads the content in the iFrame twice within a couple seconds, which results in the URL popping twice.
To resolve this, I decided to set a cookie with an expiration. Before popping, I would check to see if the cookie exists, and if it does, prevent the pop from happening.
Unfortunately, my cookie is not being set. I've read a few threads about Javascript cookies trying to figure this out. The first thing I found is Chrome does not accept cookies from local files, so I set up an IIS server to host the page.
However, the cookie still is not being set. I read this page to make sure my code was correct, and as far as I can tell, it should be correct.
The code for my page is below. Any help would be greatly appreciated.
<html>
<head>
<script type="text/javascript">
var isPopped;
function myFunction() {
alert("Hello! I am an alert box!");
}
function checkCookie() {
var user=getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:","");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
}
function pop() {
var queryString = location.search.substring(1); //Get Query String from URL of iFrame source. The substring(1) strips off the ? and only takes the first substring. This can be modified to take more and the resulting string can be edited with Regular Expressions if more flexibility is required.
var urlToPop = "https://www.google.com/#" + queryString //Set URL to pop.
var recentVisitTrue=getCookie("visitRecent");
if (recentVisitTrue != "") {
isPopped = 1;
} else {
window.open(urlToPop,"_blank");
setCookie("visitRecent", "true");
}
}
function setCookie(cName,cValue) {
var date = new Date();
date.setTime(d.getTime() + 8000000);
var expires = "; expires=" + date.toGMTString();
document.cookie = cName + "=" + cValue + expires + ";path=/";
}
function getCookie(cName) {
var name = cName + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
</script>
</head>
<body onload="pop();">
v0.32
</body>
</html>
Thanks!

unset $_POST inside javascript if statements

i need to unset $_POST['u'] but only inside a javascript function, now $_POST ARRAY unsets itself automatically after the page is ready...
function resetChat(cod_c){
if(confirm('are you sure?')){
if (sendReq.readyState == 4 || sendReq.readyState == 0) {
sendReq.open("POST", 'getChat.php?chat='+cod_c+'&last=' + lastMessage, true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.onreadystatechange = handleResetChat;
var param = 'action=reset';
param += '&user=' + document.getElementById('cod_user').value;
sendReq.send(param);
document.getElementById('txt_message').value = '';
}
<?php unset($_POST['u']); ?> // this only 1 time after confirm???
}
}
function handleResetChat() {
}
PHP is server side language and JavaScript is on client side :) You need learn more about webdev ;)

Combine multiple select field values into a single variable

I have an HTML form with multiple select dropdowns (most of the jQuery snippets I've found online talk about multi-selects which are different, i.e. checkboxes.)
How can you use jQuery to collect/join the values from all the dropdown fields, and separate them with a symbol i.e. comma or plus sign? Then, send to URL (WordPress tag search).
I've tried this code and it doesn't work:
<script>
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var tags = $(".option").map(function() {
return ($(this).text() == "") ? null : $(this).text(); // ignore default
// return $(this).text(); // include all values
}).join("+");
window.location = "<?php echo home_url( '/' ); ?>/tag/" + tags;
return false;
});
});
</script>
Here's the latest version, with .get added, still no luck:
<script>
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var tags = $(".option").map(function() {
return ($(this).text() == "") ? null : $(this).text(); // ignore default
// return $(this).text(); // include all values
}).get().join("+");
window.location = "<?php echo home_url( '/' ); ?>/tag/" + tags;
// return false; // tried this on and off
});
});
</script>
A couple things: 1) added .get() to get access to .join(), and 2) limited the return to only selected options that are not blank:
jQuery(document).ready(function() {
jQuery("#submit").click(function(evt) {
evt.preventDefault();
var tags = $(".option").map(function() {
return (this.selected && $(this).text() != "") ? $(this).text() : null;
}).get().join("+");
window.location = "<?php echo home_url( '/' ); ?>/tag/" + tags;
});
});
And for good measure, pulled in the event object and prevented the default action.
JSFiddle: http://jsfiddle.net/jKGP8/
You should use get() in combination with jquery map function, otherwise you receive not just array but jQuery object
var tags = $(".option").map(function() {
return ($(this).text() == "") ? null : $(this).text(); // ignore default
}).get().join("+");
$("#result").html(tags);
http://jsfiddle.net/tx8HT/
With on click you should just replace your old var tags = .. string with new tag calculation.
<script>
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var tags = $("#form .option").map(function() {
return ($(this).text() == "") ? null : $(this).text(); // ignore default
}).get().join("+");
window.location.href = "<?php echo home_url( '/' ); ?>/tag/" + tags;
return false;
});
});
</script>
Looks like your jQuery selector is wrong, should be "option" rather than ".option" (your code is looking for elements with the class "option"). Also, this will concatenate all select option text values, not just the ones that the user has selected. It is not clear if this is what you are after, but I thought I would give you a heads up.

how to change in below url redirect script

I have a url redirect script that works very well, but i want some change in it.
when i redirect outgoing link then at the end of redirected url i got something like '#lcinside'
example
http://dl.dropbox.com/u/36139836/unknown.html?urlc=http://imgur.com/
goes to
http://imgur.com/#lcinside
but i want to put #lcinside before url start.
example
#lcinsidehttp://imgur.com
i want to put adf url redirect link instead of #lcinside.
how can i put #lcinside before url .
below is script.
please help me.
<script>
function gup(sName)
{
sName = sName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var sRegEx = "[\\?&]" + sName + "=([^&#]*)";
var regEx = new RegExp(sRegEx);
var aResult = regEx.exec(window.location.href);
if(aResult == null)
{
return "";
}
else
{
return aResult[1];
}
}
if(gup("urlc") != "")
{
window.location = gup("urlc").replace("lcamp", "&") + "#lcinside";
}
</script>
If you are just trying to scrub the referrer, maybe you should use meta refresh
<meta http-equiv="refresh" content="1;url=http://imgur.com/#lcinside">

Categories