Check if an element exists with switch

0

Hello, how do I do a check and check if a given element exists using switch?  To be more specific, I'm trying to use this code:

var forumversion = function()
{
    invision = jQuery('#ipbwrapper').length ? "invision" : "error";
    phpbb2   = jQuery('.bodyline').length ? "phpbb2" : "error";
    phpbb3   = jQuery('#wrap').length ? "phpbb3" : "error";
    punbb    = jQuery('#pun-intro').length ? "punbb" : "error";
}
switch( forumversion )
{
            case invision: "Invision";
                break;

            case phpbb2: "phpBB2";
                break;

            case phpbb3: "phpBB3";
                break;

            case punbb: "punbb";
                break;

            default: "Erro ao identificar a versão do fórum"
}

In this case I want to check the version of certain forums .. To do this I'll check if certain specific elements of each version exist (inside the forumversion var are the elements ..) ..
 I'm new with switch yet, where could I be wrong?

    
asked by anonymous 18.04.2014 / 00:08

1 answer

4

The definition of the function is quite confusing. I believe you want to do the following:

function forumversion()
{
    if (jQuery('#ipbwrapper').length) return "invision";
    if (jQuery('.bodyline').length)   return "phpbb2";
    if (jQuery('#wrap').length)       return "phpbb3";
    if (jQuery('#pun-intro').length)  return "punbb";
    return "error";
}

Now on the switch you should compare the invocation of the function, not the function itself. forumversion will never be one of these strings, but forumversion() will be. Use this:

switch (forumversion()) {
case "invision":
    // Processamento aqui para esse tipo de forum
    break;
case "phpbb2":
    // Processamento aqui para esse tipo de forum
    break;
case "phpbb3":
    // Processamento aqui para esse tipo de forum
    break;
case "punbb":
    // Processamento aqui para esse tipo de forum
    break;
}

If you find it necessary to add defualt or a case "error" to the end options.

    
18.04.2014 / 00:28