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

How can we call javascript function from code file?

本文关键字:调用 javascript 函数 文件 代码 我们      更新时间:2023-09-26

我正在制作一个简单的机器人,它将从脚本中获取值,然后将它们放入另一个网站的文本框中,然后执行点击按钮,这样机器人就可以登录到另一个我已经编写了代码的网站,但我想在页面完全加载时调用它。我试过<body onload>,但它不起作用。

这有可能吗?我们可以在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    
}    

我不完全理解你的意思,但如果你想在文档在客户端web浏览器上完全下载后运行脚本,那么你可以使用jQuery的文档就绪事件。

将jQuery添加到您的网页中,然后在jQuery引用之后将以下脚本块添加到页面中。

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

当页面的DOM已完全加载时,将触发此脚本块。

您必须编写完整的java脚本,其中包括字符串变量中的script标记。然后可以对字符串变量进行Response.Write()。

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

试试这个

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

更新:

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
 }