Welcome to the Cumulus Support forum.

Latest Cumulus MX V4 release 4.4.2 (build 4085) - 12 March 2025

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Legacy Cumulus 1 release 1.9.4 (build 1099) - 28 November 2014
(a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

If you are posting a new Topic about an error or if you need help PLEASE read this first viewtopic.php?p=164080#p164080

Question for the java script junkies

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
User avatar
gwheelo
Posts: 453
Joined: Wed 11 Jun 2008 7:36 pm
Weather Station: WMR-88
Operating System: Windows 8.1
Location: L'Estartit, Spain
Contact:

Question for the java script junkies

Post by gwheelo »

The following script works, but if I substitute the switch with just:

LANG = lang_opt;

any following code fails - why? What have I missed about variables and assignment? LANG is a global variable.

lang = new Array ("gb","es","ct","fr","nl","de" );

lang_opt = lang[0]; // pre-set a value
lang_opt_old = lang[0]; // pre-set a value
lang_count = lang.length;

// find out if we have come from a non-native page
posted_opt = " " + this.location;
find_it = posted_opt.indexOf('?');
posted_opt = posted_opt.substring(find_it +1); // returns everything after '?' in url
if(posted_opt.length >= 2) select_lang(posted_opt );


function select_lang(new_opt) {
for(lang_x = 0 ; lang_x <lang_count ; lang_x ++) {
if(new_opt == lang[lang_x] && document.getElementById(lang[lang_x]) ) { // valid entry found
lang_opt_old = lang_opt;
lang_opt = new_opt;
langX= lang_opt;
if(flag_exist) set_flag();
document.getElementById(lang_opt_old).style.display="none";
document.getElementById(lang_opt).style.display="inline";
new_lang = false;
break;
}
}

} // END function select_lang()

switch(lang_opt)
{
case(lang_opt = "gb"):
LANG = gb;
break;
case (lang_opt = "ct"):
LANG = ct;
break;
case (lang_opt = "es"):
LANG = es;
break;
case (lang_opt = "fr"):
LANG = fr;
break;
case (lang_opt = "nl"):
LANG = nl;
break;
case (lang_opt = "de"):
LANG = de;
break;
}

changeLang();

} // END function set_flag
</script>
Image
User avatar
mcrossley
Posts: 14388
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: Question for the java script junkies

Post by mcrossley »

George, it is because lang_opt just contains the string 'gb', 'de' etc, but the switch statement is making LANG equal to the gb , de 'object'.
User avatar
gwheelo
Posts: 453
Joined: Wed 11 Jun 2008 7:36 pm
Weather Station: WMR-88
Operating System: Windows 8.1
Location: L'Estartit, Spain
Contact:

Re: Question for the java script junkies

Post by gwheelo »

LANG equal to the gb , de 'object'
OK - I will spend some time learning the difference between "strings" and "objects."

In the meantime - as you obviously see - I am attempting to replace the multiple line switch with a single line - any suggestions - and as a bonus perhaps the alternative will help me understand what I have missed.

Thanks for your help!

GW
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Question for the java script junkies

Post by beteljuice »

A few things ars* about face.

First of all you are using the switch statement as an 'if' and it should be ==, but the logic can be easier.

Code: Select all

switch(lang_opt)
{
case "gb":
LANG = gb; // this is a variable assignment NOT a string !
break;
case "ct":
LANG = ct; // this is a variable assignment NOT a string !
break;

// etc.

Buy you don't need any of that ! - you don't say at what point LANG is required, so ....

Code: Select all

lang_opt = LANG = lang[0]; // pre-set a value

...
function ....

lang_opt_old = lang_opt;
lang_opt = LANG = new_opt; // changed
langX= lang_opt;

Image
......................Imagine, what you will KNOW tomorrow !
User avatar
mcrossley
Posts: 14388
Joined: Thu 07 Jan 2010 9:44 pm
Weather Station: Davis VP2/WLL
Operating System: Bullseye Lite rPi
Location: Wilmslow, Cheshire, UK
Contact:

Re: Question for the java script junkies

Post by mcrossley »

Good catch on the switch statement beteljuice, but the assignments of LANG will not work. I recognise this as being an extension to my SteelSeries scripts, the LANG object contains all the localisation strings as named attributes (eg LANG.temperatureTitle) so setting that variable to a string is not going to work.

The only way I can see that you can get rid of the switch statement would be to use an eval(), which is generally frowned upon, though I make use of them now and again (and I see the Ajax script libraries use also!)
User avatar
gwheelo
Posts: 453
Joined: Wed 11 Jun 2008 7:36 pm
Weather Station: WMR-88
Operating System: Windows 8.1
Location: L'Estartit, Spain
Contact:

Re: Question for the java script junkies

Post by gwheelo »

Right you are Mark!
I recognize this as being an extension to my SteelSeries scripts
Plus a much earlier multi-language script also lifted from this forum (JSFX_FloatDiv).

I have rewritten the switch to conform to convention in spite of the fact that my unconventional version also worked (why?).

With my limited java script ability - I simply assumed there was a single line of code to replace the "Switch" - since beteljuice always finds a shorter version for any code he sees. I obviously need to spend a great deal more time understanding "objects".

Here is the corrected (I think) code.

switch(lang_opt)
{
case "gb":
LANG = g_b;
break;
case "ct":
LANG = c_t;
break;
case "es":
LANG = e_s;
break;
case "fr":
LANG = f_r;
break;
case "nl":
LANG = n_l;
break;
case "de":
LANG = d_e;
break;
}

GW
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Question for the java script junkies

Post by beteljuice »

I don't really see what you are going to do with that, it just seems to be replication of existing data.

I have absolutely no idea about 'canvass' nor Marks script, so I don't know if it can accept variables "on-the-fly" or not. I expect that it could take a 'posted' variable ?

Looking at the code, you are still populating your pages with ALL languages and hiding / displaying as necessary. I suspect this would NOT be a good idea for "canvass".

So ....
If the "canvass" will accept 'posted' vars BUT can't work on-the-fly, then you would need to recall the (same) page with the (new) lang_opt.

If "canvass" CAN be rebooted on-the-fly" - Have just one instance of it, don't give it a lang_opt 'id' - give it a unique 'id' (if it doesn't already have one).

Then add whatever is necessay here:

Code: Select all

function select_lang(new_opt) {
  for(lang_x = 0 ; lang_x <lang_count ; lang_x ++) {

// NEW for canvass - psuedo code !!
    if(new_opt == lang[lang_x] && document.getElementById(CANVASS_ID]) ) { // canvass  found
       REBOOT CANVASS WITH new_opt in some way
    }
// END NEW

    if(new_opt == lang[lang_x] && document.getElementById(lang[lang_x]) ) { // valid entry found
       lang_opt_old = lang_opt;
       lang_opt = new_opt;
       langX= lang_opt;
       if(flag_exist) set_flag();
       document.getElementById(lang_opt_old).style.display="none";
       document.getElementById(lang_opt).style.display="inline";
       new_lang = false;
       break;
     }
  }
} // END function select_lang()
NOTE: I expect Mark to correct me about this :lol: :clap: :roll:
I'm not sure how the canvass would 'initialise' !
Image
......................Imagine, what you will KNOW tomorrow !
User avatar
gwheelo
Posts: 453
Joined: Wed 11 Jun 2008 7:36 pm
Weather Station: WMR-88
Operating System: Windows 8.1
Location: L'Estartit, Spain
Contact:

Re: Question for the java script junkies

Post by gwheelo »

Mark and betel -
I don't really see what you are going to do with that
The real problem here is simply ignorance (on my part) and a ragout of scripts - all for one page.

Yes there is canvas - yes there is hiding - yes there is Java script refresh - yes there are routines from many sources, methods, and styles - all on one page. I just can't say no when I see something intriguing. Oh! did I mention the two seperate "Gauge" libraries. Considering all that and my own stumbling code it is a wonder that the page even loads. What I need to do is to start over fresh and write from one viewpoint - but I bet it will never happen - mainly because I don't have the background or experience to create an entire page on my own (also - lazy). I do have a good time, however, and have learned from every mistake, every script offered here on the forum, and the great help I have received from so many.

Still not finished - but now live: http://www.wheelocknet.net/cumulus/gauges.html?gb

GW
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Question for the java script junkies

Post by beteljuice »

It works :clap: - but I haven't got a clue how :oops:

BTW - flags code, change cursor: hand for cursor: pointer (for FireFox)
Image
......................Imagine, what you will KNOW tomorrow !
Post Reply