Something wrong with JavaScript

This is a Javascript that runs when i click a specific tab.

For some reason, when i have -Session.DataHasChanged2- as True, if i click the tab i receive the alert message "3", and then the message "Tem dados por gravar, quer prosseguir".

This means that even if i click on cancel (result of the confirm), it already started to run the action SaveAsTemporary. So the Cancel will do nothing,

I checked and the session value changes to true (the refresh is made). Is anything wrong with my code? Thanks


"<script>

$('div[name=MyTab]').click(function()    

{

    if('" + Session.DataHasChanged2 + "' == 'True')

    {

       if(confirm('Tem dados por gravar, quer prosseguir ?')) {

            $('#"+SaveAsTemporary.Id+"').click();

            alert('1');

    }


    else

{

        return False;

}


    }


   if('" + Session.DataHasChanged2 + "' == 'False')

    {      


        $('#"+SaveAsTemporary.Id+"').click();

        alert('3');

    }

    else

        alert('4')

});

</script>"

Hi André,

Can you try this js code : 

"<script>

$('div[name=MyTab]').click(function()    

{

    if('" + Session.DataHasChanged2 + "' == true)

    {

       if(confirm('Tem dados por gravar, quer prosseguir ?')) {

            $('#"+SaveAsTemporary.Id+"').click();

            alert('1');
            return true;

    }

    else

{

        return False;

}

    }



   if('" + Session.DataHasChanged2 + "' == false)

    {      



        $('#"+SaveAsTemporary.Id+"').click();

        alert('3');
        return true;

    }

    else

 {

        alert('4');

       return False;

}

});

</script>"

Solution

Hi Andre,

i believe this is working with default values, when you udpate session value you must refersh the script.


Thanks,
Balu


Solution

"I checked and the session value changes to true (the refresh is made). Is anything wrong with my code? "


That is already done.


Thanks anyway

Pedro Vila Nova wrote:

Hi André,

Can you try this js code : 

"<script>

$('div[name=MyTab]').click(function()    

{

    if('" + Session.DataHasChanged2 + "' == true)

    {

       if(confirm('Tem dados por gravar, quer prosseguir ?')) {

            $('#"+SaveAsTemporary.Id+"').click();

            alert('1');
            return true;

    }

    else

{

        return False;

}

    }



   if('" + Session.DataHasChanged2 + "' == false)

    {      



        $('#"+SaveAsTemporary.Id+"').click();

        alert('3');
        return true;

    }

    else

 {

        alert('4');

       return False;

}

});

</script>"


nop. but thanks

André,

Balu is right.

Your expression is evaluated when you render the screen, at that moment the session variable will be concatenated with the rest of the javascript text and placed on your html page. This means that when you register the click function, it will have the value of the session variable at the time of rendering the screen, not the value of the session variable when you click on it.

If you want your script to reflect changes to the session variable, you need to either re-render the entire screen or at least Ajax Refresh the portion where your script is, so your expression is re-evaluated.

Jorge Martins wrote:

André,

Balu is right.

Your expression is evaluated when you render the screen, at that moment the session variable will be concatenated with the rest of the javascript text and placed on your html page. This means that when you register the click function, it will have the value of the session variable at the time of rendering the screen, not the value of the session variable when you click on it.

If you want your script to reflect changes to the session variable, you need to either re-render the entire screen or at least Ajax Refresh the portion where your script is, so your expression is re-evaluated.


I know Jorge, i refreshed everything as i show in the image below. The problem is that the script runs 2 times, one with false and the other with the correct value. i don't understand why i get 2 different values to the same session variable and why the script runs twice.

Hi André,

"<script>

$('div[name=MyTab]').click(function()    

{

    if('" + DataHasChanged2 + "' == 'True')

    {

       if(confirm('Tem dados por gravar, quer prosseguir ?')) {

            $('#"+SaveAsTemporary.Id+"').click();

            alert('1');
            return true;

    }

    else

{

        return False;

}

    }



   if('" + DataHasChanged2 + "' == 'False')

    {      



        $('#"+SaveAsTemporary.Id+"').click();

        alert('3');
        return true;

    }

    else

 {

        alert('4');

       return False;

}

});

</script>"


It's working for me.



if i change for false : 


I needed to send via notify to refresh the tab.