当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - 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.

Is this possible that we could call our javascript function from our c# code file under the documentcompleted event or any other way?

<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.

Add jQuery to your web page and then add the following script block to the page after the jQuery reference.

<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.

网友答案:

You have to write the complete java script including script tag in a string variable. Then you can Response.Write() the string variable.

string script = "<script type="" language=""> YOUR JAVASCRIPT </script>";
Response.Redirect(script);
网友答案:

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
 }
分享给朋友:
您可能感兴趣的文章:
随机阅读: