I am working on localization and use cookie to store language. I have a Select
:
var select = new dijit.form.Select({
name: "languageSelect",
onChange: function (value) {
changeLanguage(value);
}
}, "languageSelect");
in onChange
event, will call method changeLanguage
:
var changeLanguage = function (language) {
$.ajax({
type: "PUT",
url: "/api/accounts/changelanguage?language=" + language,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
window.location.reload(false);
}
});
};
This method with do ajax call to server and then re-load page.
The problem is: on the page load, I need to read cookie language
which user chose before and set it to Select
:
select.set("value", readCookie("language"));
But, this set
causes the event OnChange
fire, then make page re-load which I don't want at the beginning. I tried to use one flag like below but it does not work out:
var select = new dijit.form.Select({
name: "languageSelect",
style: { width: '200px' },
onChange: function (value) {
if (isLoading == false)
changeLanguage(value);
}
}, "languageSelect");
var isLoading = true;
select.set("value", readCookie("language"));
isLoading = false;
Is there anyway to sort it out, I was thinking to prevent event OnChange
fire on set
method, but have not found out how to do it.
I am working on localization and use cookie to store language. I have a Select
:
var select = new dijit.form.Select({
name: "languageSelect",
onChange: function (value) {
changeLanguage(value);
}
}, "languageSelect");
in onChange
event, will call method changeLanguage
:
var changeLanguage = function (language) {
$.ajax({
type: "PUT",
url: "/api/accounts/changelanguage?language=" + language,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
window.location.reload(false);
}
});
};
This method with do ajax call to server and then re-load page.
The problem is: on the page load, I need to read cookie language
which user chose before and set it to Select
:
select.set("value", readCookie("language"));
But, this set
causes the event OnChange
fire, then make page re-load which I don't want at the beginning. I tried to use one flag like below but it does not work out:
var select = new dijit.form.Select({
name: "languageSelect",
style: { width: '200px' },
onChange: function (value) {
if (isLoading == false)
changeLanguage(value);
}
}, "languageSelect");
var isLoading = true;
select.set("value", readCookie("language"));
isLoading = false;
Is there anyway to sort it out, I was thinking to prevent event OnChange
fire on set
method, but have not found out how to do it.
1 Answer
Reset to default 9try adding false
, like:
select.set("value", readCookie("language"));
to
select.set("value", readCookie("language"), false);
false
flag at the end of the set()
method that will prevent the onChange
event.