简体   繁体   English

从codebehind(c#)调用JavaScript函数不起作用

[英]Call JavaScript function from codebehind(c#) is not working

I am trying to call a Javascript from the codebehind but form some reason it is not working. 我正在尝试从背后的代码中调用Javascript,但由于某种原因,它无法正常工作。 Let me explain what i am trying to accomplish: when the page loads the system needs to check if this is the first time the user visit this page. 让我解释一下我要完成的工作:当页面加载时,系统需要检查这是否是用户第一次访问此页面。 if so a lightbox will open. 如果是这样,一个灯箱将打开。 So I created a javascript function in the page the onPageLoad i would like to call this function if it is necesary. 因此,我在onPageLoad页面中创建了一个javascript函数,如果需要的话,我想调用此函数。 This is what I have so far, looks pretty straight forward but it is not working. 这是我到目前为止的内容,看起来很简单,但是没有用。 I will appreciate any help. 我将不胜感激。

Here is the 这里是

html: 的HTML:

  <form id="form1" runat="server">
  <div>

  <a id="OpenTutorial"  href="../lightwindow/step1.html" params="lightwindow_width=450,lightwindow_height=470" class="lightwindow page-options">Open Tutorial</a>


  </div>


  <script>
    function OpenTutorial() { $("#OpenTutorial").click() }
   </script>

  </form>

Code behind: 后面的代码:

    protected void Page_Load(object sender, EventArgs e)
    {

  //code to check if this is the first time
  ..... 
  // it this is the first time, call this function 
   Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "OpenTutorial()", true);

    }

Change the id or the name of the function so they are different, they are clashing in the global namespace. 更改id或函数名称,以使它们不同,它们在全局名称空间中冲突。

<a id="anc_OpenTutorial" />

<script>
    function OpenTutorial() { $("#anc_OpenTutorial").click() }
</script>

OR just call clicking the link with the code instead of calling the function. 或仅调用单击带有代码的链接,而不调用函数。

TO follow the link change it to access the DOM element 要跟踪链接,请将其更改为访问DOM元素

$("#anc_OpenTutorial")[0].click() 

or 要么

$("#anc_OpenTutorial").get(0).click() 

也许尝试使用jQuery的触发函数,即:

function OpenTutorial() { $("#OpenTutorial").trigger('click'); }

How about refactoring your javascript function as follows? 如何重构您的javascript函数,如下所示?

<form id="form1" runat="server">
<div>

<a id="OpenTutorial"  href="../lightwindow/step1.html" params="lightwindow_width=450,lightwindow_height=470" class="lightwindow page-options">Open Tutorial</a>

</div>


<script>
  // Function that Opens the Tutorial
  function OpenTutorial() { 
      // Using colorbox for an example, but you can start the lightbox through the function
      $('#OpenTutorial').colorbox(); 
  }

  $("#OpenTutorial").click(function(){ OpenTutorial() });
 </script>

</form>

Code Behind: 背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
   //code to check if this is the first time
   ..... 
   // it this is the first time, call this function 
   Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "OpenTutorial()", true);
}

EDIT: Updating the OpenTutorial function to start a lightbox as opposed to opening the link 编辑:更新OpenTutorial函数以启动灯箱,而不是打开链接

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

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