locked
call Javascript Function located from CodeBehind RRS feed

  • Question

  • User944339287 posted

    Hi guys.. i wanna call javascript function which placed at Master Page from Content Page but failed. 

    Below is my code. Please correct me. TQ

    Master Page (HTML)

    <head>
    <script>
        function openNav() {
            document.getElementById("mySidenav").style.width = "350px";
        }
    </script>
    </head>
    <body>
        <span style="cursor: pointer" onclick="openNav()">Open</span>
    </body>

    Content Page (Code Behind)

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Page.IsPostBack = False Then
    
                Page.ClientScript.RegisterStartupScript([GetType](), "CallFunction", "openNav()", True)
    
            End If
    
        End Sub





    Thursday, May 7, 2020 10:15 AM

Answers

  • User475983607 posted

    I test your design and the function call works.  I don't have a mySidenav and you did not share this part of the code so I wrote to the console. 

    <script>
        function openNav() {
            console.log("Hello World");
        }
    </script>

    I assume the selector you are using is incorrect or there's a CSS conflict.  I suppose there could be an error too but you have not explained what "failed" means.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 11:02 AM

All replies

  • User475983607 posted

    I test your design and the function call works.  I don't have a mySidenav and you did not share this part of the code so I wrote to the console. 

    <script>
        function openNav() {
            console.log("Hello World");
        }
    </script>

    I assume the selector you are using is incorrect or there's a CSS conflict.  I suppose there could be an error too but you have not explained what "failed" means.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 11:02 AM
  • User944339287 posted

    Ya. It's working when i put into other pages. TQ

    Thursday, May 7, 2020 11:56 PM