Question

I have url that goes like this

http://wwww.example.com/eng/about/what

I want to have new var like this in Jquery, that is working in IE8

var lang ="eng";

And replace innerHTML of element, for now i have something like this

 $(document).ready(function () {
        var lang = "ENG";
        $(".dropdown-toggle").html(lang + "<b class='caret'></b>");

    });'

The problem is that var eng can we somethin else, i have tried with indexOF, but no luck? And if there is no value inside i must use default

var lang = "eng";

EDITED

For now we have made it to this

$(document).ready(function () { var url = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname; var arr = url.split("/"); var lang = (arr[1] != undefined) ? arr[1] : "en";

});

The problem is on default loading of index page, it means, what some one get on home page Hi only gets

http://wwww.example.com/

But sometime he gets

http://wwww.example.com/fr 

or something else depends on language

Was it helpful?

Solution

If your url structure is same, you can use this;

var url = "http://wwww.example.com";
//var url = "http://wwww.example.com/fr/about/what";
var arr = url.split("/");
var lang = (arr[3] != undefined && (arr[3].length == 2 || arr[3] == "eng") ) ? arr[3]: "eng";

Here is a working demo: jsfiddle

OTHER TIPS

I think this will work:

var lang = location.pathname.match(/^\/(.+?)\//)[1];

For example if current URL is http://wwww.example.com/fr/about/what lang will be fr.

if the URL is http://wwww.example.com/eng/about/what then,

document.URL.split("/")[3]

You can use either of plugin https://github.com/allmarkedup/purl or http://www.kevinleary.net/jquery-parse-url/ to get query string params and uri segments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top