简体   繁体   English

javascript函数调用而不单击超级链接按钮为何

[英]javascript function call without click hyper link button why

Three hyperlink button and i call a javascript function to onclick button ,i store the link button id in respective session after check the condition but the function are call without click hyper link button and it allweys execute last session values 三个超链接按钮,我调用一个javascript函数来onclick按钮,我在检查条件后将链接按钮id存储在相应的会话中,但是调用该函数时无需单击超级链接按钮,它会执行最后一个会话值

<li><a href="" id="link_leave" onclick="showBtton(this.id)">Leave Policy</a></li>
 <li><a href="" id="link_tour" onclick="showBtton(this.id)">Tour policy </a></li>
 <li><a href="" id="link_it" onclick="showBtton(this.id)">IT Policy</a> </li>

java script function call Java脚本函数调用

<script lang="javascript" >

    {
        var itsPostBack = <%=Page.IsPostBack ? "true" : "false" %>;
        alert(itsPostBack);
        if(itsPostBack!=false)
        {
            function showBtton(id) 
            {


                if ("link_leave"==id) 
                {
                    var a = "leave_button";
                    alert(a);

                    <%--  '<%Session["identify"] = "' + a + '"; %>';  --%>
                    '<%Session["identify"] = "leave_button"; %>';

                    var Session_value = "";
                    Session_value = '<%=Session["identify"] %>'; 
                    Session_value =Session["identify"] ;
                    alert(Session_value);
                    javascript: window.open('policy.aspx', '_blank');
                }
                if ("link_tour"==id)
                {
                    var a = "tour_button";
                    alert(a);

                    <%--  '<%Session["identify"] = "' + a + '"; %>'; --%>
                    '<%Session["identify"] = "tour_button"; %>';

                    var Session_value = "";
                    Session_value = '<%=Session["identify"] %>';
                    Session_value = Session["identify"];
                    alert(Session_value);
                    javascript: window.open('policy.aspx', '_blank');
                }

                if ("link_it"==id)
                {
                    var a = "it_button";
                    alert(a);

                    <%--  '<%Session["identify"] = "' + a + '"; %>'; --%>
                    '<%Session["identify"] = "it_button"; %>';

                    var Session_value = "";
                    Session_value = '<%=Session["identify"] %>';
                    Session_value = Session["identify"];
                    alert(Session_value);
                    javascript: window.open('policy.aspx', '_blank'); 

                }

            } 
        }
    }

    </script>

The issue is you are trying to modify the server side ASP.NET session using Javascript which is not possible as once you run Javascript code it is run on the clients browser which has no direct access to the session on the server. 问题是您正在尝试使用Javascript修改服务器端ASP.NET会话,因为一旦运行Javascript代码,它将在无法直接访问服务器上会话的客户端浏览器上运行,因此无法进行此操作。

You have a few choices: 您有几种选择:

  1. Send the button ID to the server via an AJAX call and then retrieve it via an AJAX call when needed 通过AJAX调用将按钮ID发送到服务器,然后在需要时通过AJAX调用进行检索
  2. Use local session storage to implement a client side session which is stored in the browser rather than the server so you can modify it through Javascript. 使用本地会话存储来实现客户端会话,该客户端会话存储在浏览器而不是服务器中,因此您可以通过Javascript对其进行修改。 Please see this page for more details: https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage 请参阅此页面以获取更多详细信息: https : //developer.mozilla.org/en/docs/Web/API/Window/sessionStorage
  3. Another option would be to pass the button id as a query string parameter in the URL so you can avoid the session. 另一个选择是将按钮id作为查询字符串参数传递到URL中,以便避免会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM