Forum Moderators: open

Message Too Old, No Replies

Set cookie and load new page

         

tkmorrison

5:04 pm on Jun 21, 2009 (gmt 0)

10+ Year Member



I have inherited the following code which uses a drop down menu to set the language cookie for a site.

I need to add code to load a new page when a language is selected - it's the same page for all languages.

Any way to modify this code to add the page reload?

Thanks!

Terry

<form action="" method="post" name="languageform" id="languageform">
<img src="/images/flag_us.png" class="img-navflag" name="flag_img"/>
<select id="lang_user" onchange="javascript:processCookie()" name="lang_user">
<option value="en-us">ENGLISH</option>
<option value="de-at">DEUTSCH</option>
</select>

<script type="text/javascript" language="javascript">
var flag_img_arr=new Array();
flag_img_arr[0]="/images/flag_us.png"
flag_img_arr[1]="/images/flag_germany.png"
tl = getCookie("lang_cookie");
for(i=0;i<6;i++){
if(tl==document.languageform.lang_user.options[i].value){
document.images.flag_img.src=flag_img_arr[i];
document.getElementById("lang_user").selectedIndex=i;
}
}//for
</script>

</form>

dreamcatcher

8:26 pm on Jun 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi tkmorrison, a warm welcome to Webmaster World. :)

Maybe use window.location after the function is processed. Something like this might be ok.

<select id="lang_user" onchange="javascript:processCookie();window.location='page.html'" name="lang_user">

dc

tkmorrison

10:45 pm on Jun 21, 2009 (gmt 0)

10+ Year Member



That didn't work - it sets the cookie but doesn't reload that new page.

But this comes close:

<select id="lang_user" onchange="javascript:processCookie();window.location.replace('page.html')" name="lang_user"

This loads page.html in the current language - but displays the new language in the drop down. Then on reload, or navigating to any other page, it displays the page in the new language.

It sets the cookie but doesn't read it until a reload.

Any thoughts?

Thanks!

Terry

dreamcatcher

7:04 am on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can`t you just add the window.location directive to the processCookie() function itself?

dc

daveVk

7:31 am on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try location.reload(true)

tkmorrison

10:34 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



Thanks for the help - I seem to be getting somewhere.

The processCookie() function now looks like this:

function processCookie(){
document.languageform.submit();
lang=document.languageform.lang_user.value;
delCookie('lang_cookie');
setCookie('lang_cookie',lang,'1');
window.location.href="newpage.html";
}

Which works - almost.

When a new language is selected, it sets the cookie and loads newpage.html in that language.

However, the next page navigated to loads in the old language.

But a reload of that page displays the new language - and all subsequent pages are in the new language.

Any thoughts on what might be going on?

Thanks a lot!

Terry

daveVk

12:38 am on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it's the same page for all languages

If the browser has one of your pages cached say widget.html in english, then future requests for widget.html will get this page even if the cookie has changed.

Either use different urls per language, (eg widget.html?lang=eng) or on page load check page is in correct language else reload.