简体   繁体   English

在重定向到另一个页面之前,在JavaScript的背后调用JavaScript函数

[英]Call JavaScript Function On Call Behind of Javascript before redirecting to another page

I have Javascript function which i need to call at code behind in vb.net . 我有Javascript函数,需要在vb.net后面的代码处调用。 The Main problem is it is not called properly because there is redirection to next page before this function executes. 主要问题是未正确调用它,因为在执行此功能之前存在重定向到下一页的信息。 I do not want to call this function on Onclick event of button or something like that i want to call it after some specific condition in code behind. 我不想在按钮的Onclick事件或类似的事情上调用此函数,我想在后面的代码中某些特定条件后调用它。 I have already tried following solutions please let me know if you have some other suggestions. 我已经尝试过以下解决方案,如果您还有其他建议,请告诉我。

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Me.GetType(), "Script", "ShowEntryPermForm();", True)

Page.ClientScript.RegisterStartupScript(Me.GetType(), "Window", "ShowEntryPermForm();", True)

Problem Description: added from comment 问题描述:从评论中添加

The scenario is that in javascript function i have checked various conditions related to the project and it open up a pop up window where user enter his or her comments. 该场景是在javascript函数中我检查了与项目相关的各种条件,并打开了一个弹出窗口,用户可以在其中输入他或她的评论。

It is not necessary every time the function will open the pop up window. 不一定每次函数都会打开弹出窗口。 But by default the page redirects to new page as both the functions Javascript and redirection to another window are run on click event of the button. 但是默认情况下,页面的重定向到新页面,因为Javascript功能和重定向到另一个窗口都在按钮的click事件上运行。

But now my requirement is changed now i have to call the function from code behind in particular condition. 但是现在我的要求发生了变化,现在我不得不从特定条件下的代码中调用该函数。 Hope you get it now 希望你现在明白

You can do this in following way. 您可以按照以下方式进行操作。

window.onbeforeunload = function()
{
    //your code goes here
    return "";
}

If you return anything here then browser will display and ask user whether to leave the page. 如果您在此处返回任何内容,则浏览器将显示并询问用户是否离开页面。 In your case you don't need to return anything but runs the code. 在您的情况下,除了运行代码外,您无需返回任何内容。

The only way is to Separate the code and javascript in the way they will not rely on each other 唯一的方法是将代码和javascript以彼此不依赖的方式分开

for example you make an hidden asp:button with the rest of server side code 例如,您使用其他服务器端代码制作了一个隐藏的 asp:button

server side : 服务器端 :

if(...) then
page.ClientScript.RegisterStartupScript(Me.GetType(), "Window", "ShowEntryPermForm();", True)
else
  page.ClientScript.RegisterStartupScript(Me.GetType(), "Window", "otherfunction();", True)
    end if

sub IDServersideHiddenbutton(obj as object ,e as eventargs)handles IDServersideHiddenbutton.click
rest of the server side code including the redirect
end sub

client side : 客户端 :

   javascript ShowEntryPermForm()
        {
      //  javascript code

    $("#IDServersideHiddenbutton").click();
    }

javascript otherfunction()
{
//just the click 

$("#IDServersideHiddenbutton").click();
}

I really don't know your code structure but You can manipulate it to work with this idea 我真的不知道您的代码结构,但是您可以操纵它来实现此想法

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

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