简体   繁体   English

我们如何从代码文件中调用javascript函数?

[英]How can we call javascript function from code file?

I am making simple bot which will take values from script then it will put them into the textboxes of another website and then perform click on button so the bot can log in another website I have written the code, but I want to call it when page is fully loaded. 我正在制作一个简单的机器人,它将从脚本中获取值,然后将其放入另一个网站的文本框中,然后执行单击按钮,以便该机器人可以登录我编写了代码的另一个网站,但是我想在页面上调用它已满载。 I tried <body onload> but it doesn't work. 我尝试了<body onload>但是它不起作用。

Is this possible that we could call our javascript function from our c# code file under the documentcompleted event or any other way? 是否有可能在documentcompleted事件下以其他方式从c#代码文件中调用javascript函数?

<script type="text/javascript">
    function myf() {
        document.getElementsByTagName("INPUT")[1].setAttribute("value", "userabc");
        document.getElementsByTagName("INPUT")[2].setAttribute("value", "passwordabc");
        document.getElementsByTagName("INPUT")[3].click();
    }
</script>

//C# Code file
protected void Page_Load(object sender, EventArgs e)
{
    Response.Redirect("https://www.iauc.co.jp/auction/prelogin01_en.jsp?timestamp=1360652615774");
}

protected void Page_LoadComplete(object sender, EventArgs e)
{
    //Can we call javascript function here    
}    

I did not fully understand you but if you want to run a script when the document has been fully downloaded on the client web browser, then you can user jQuery's document ready event. 我不太了解您,但是如果您要在客户端Web浏览器上完全下载文档后运行脚本,则可以使用jQuery的文档就绪事件。

Add jQuery to your web page and then add the following script block to the page after the jQuery reference. 将jQuery添加到您的网页,然后在jQuery参考之后将以下脚本块添加到该页面。

<script type="text/javascript">
    $(function () {
        // write your JavaScript code here
        myf();
    });
</script>

This script block will get triggered when the DOM for the page has been fully loaded. 页面的DOM完全加载后,将触发此脚本块。

Try this 尝试这个

// here's will be your script
string script = "";
ClientScriptManager clientScript = Page.ClientScript;
clientScript.RegisterStartupScript(typeof(Page), "a key", script);

UPDATE : 更新:

this will check it if its fully loaded and then do what you want


$(document).ready(function() 
 {
   //page is fully loaded and ready, do what you want
 }

You have to write the complete java script including script tag in a string variable. 您必须在字符串变量中编写包括脚本标签在内的完整Java脚本。 Then you can Response.Write() the string variable. 然后,您可以Response.Write()字符串变量。

string script = "<script type="" language=""> YOUR JAVASCRIPT </script>";
Response.Redirect(script);

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

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