locked
Call a java script function with para meters in code behind RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    I have a java script function 

    <script type="text/javascript">
    
             function SubmitsEncry(mykey,myiv ) {
                 debugger;
                 var txtUserName = $('#tbx_UserName').val();
                 var txtpassword = $('#tbx_Password').val();
    
                 if (txtUserName == "") {
                     alert('Please enter UserName');
                     return false;
                 }
                 else if (txtpassword == "") {
                     alert('Please enter Password');
                     return false;
                 }
                 else {
                    
                     var key = CryptoJS.enc.Utf8.parse(mykey);
                     var iv = CryptoJS.enc.Utf8.parse(myiv);
    
                     $('#HDUser').val(txtUserName);
    
                     var encryptedpassword = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(txtpassword), key,
    
                     { keySize: 128 / 8, iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    
                     $('#HDpass').val(encryptedpassword);
    
                     document.getElementById("hduser").value = encryptedlogin;
                     document.getElementById("hdpass").value = encryptedpassword;
                    
                 }
             }
        </script>

    I am trying to call this function in my code behind but it doesn't work. (although it works when I call it from client side.)

    for code behind I am am doing like below

     Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "SubmitsEncry("+mykey+","+myiv+")", true);

    Please suggest 

    Wednesday, July 3, 2019 9:30 AM

Answers

  • User475983607 posted

    The JavaScript syntax is incorrect.   You have a lot of experience in these forums.  IMHO, you should be automatically checking the Browser's Dev Tools for JavaScript errors.

    The following syntax assumes the two function arguments are strings.

    Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "SubmitsEncry('"+mykey+"','"+myiv+"')", true);	

    If one argument is an int then remove the single quotes.

    Secondly, you have not shared how mykey and myiv are populated if at all.    Please set a break point  (the Visual Studio debugger) to verify the code is functioning as expected.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 11:46 AM

All replies

  • User475983607 posted

    The JavaScript syntax is incorrect.   You have a lot of experience in these forums.  IMHO, you should be automatically checking the Browser's Dev Tools for JavaScript errors.

    The following syntax assumes the two function arguments are strings.

    Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "SubmitsEncry('"+mykey+"','"+myiv+"')", true);	

    If one argument is an int then remove the single quotes.

    Secondly, you have not shared how mykey and myiv are populated if at all.    Please set a break point  (the Visual Studio debugger) to verify the code is functioning as expected.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 11:46 AM
  • User-474980206 posted

    not sure the point of calling this routine on page render. the password field will always be empty and unless the server fills in the username it will be also.

     

    Wednesday, July 3, 2019 2:11 PM