1

I want to check if url of the website not contain ?lang=en ex.

mywebsite.com

to append mywebsite.com?lang=en using jQuery

Can someone pls give me example how to do this code.

Thanks,

3 Answers 3

2
// Check the query portion of the URL
if ( !location.search ) location.search = 'lang=en';
// If it has a query string, and not lang=en then - 
else if ( !location.search.match(/lang\=en/g) ) location.search+= '&lang=en';
Sign up to request clarification or add additional context in comments.

Comments

1
if(window.location.href.indexOf('?lang=en') < 0){
   window.location.href = window.location.href + '?lang=en';
}

Comments

0

Here's a function i'm using in my current project, I don't remember the source where i found it but hope it can help:

function setGetParameter(paramName, paramValue)
{
var url = window.location.href;
if (url.indexOf(paramName + "=") >= 0)
{
    var prefix = url.substring(0, url.indexOf(paramName));
    var suffix = url.substring(url.indexOf(paramName)).substring(url.indexOf("=") + 1);
    suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
    url = prefix + paramName + "=" + paramValue + suffix;
}
else
{
if (url.indexOf("?") < 0)
    url += "?" + paramName + "=" + paramValue;
else
    url += "&" + paramName + "=" + paramValue;
}
window.location.href = url;
}
setGetParameter("lang","en")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.