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
}
Related
I am using the below code to call a javascript function from aspx page load. But it's not working.
protected void Page_Load(object sender, EventArgs e)
{
btn_add_more_pack.Attributes.Add("OnClick", "openform()");
}
The javascript :
function openform()
{
try {
alert('enter');
}
catch (ex) {
}
}
I think the problem could be OnClick it should be onclick though it doesn't matter (not case sensitive) but you can try like below.
So on page load it should be as follows
protected void Page_Load(object sender, EventArgs e)
{
btn_add_more_pack.Attributes.Add("onclick", "openform()");
}
I have copied your code and tried in my system. Everything is working fine. But, one catch here:
I am using VS-2010 to work on your item.
Where have you added your javascript function? Is it inside .aspx page head section or a separate file?
If you have added the above Javascript function inside .aspx page head section, then you don't need to alter anything. Everything works fine.
But, if you have added the above Javascript function inside a separate .js file, you should remember to add reference in the .aspx head section like below
<script src="pathToJavascriptFile.js" type="text/javascript"></script>
I have a script that I want to pop a window after 5 page views. The java script works fine on the default.aspx page with a link to call it. But I want to launce it from my default.aspx.cs page after my session var count gets to 5. How can I do this? Is it possible?
default.aspx
<script type="text/javascript">
window.name = "Register";
function popWin(link) {
var w = window.open(link.href, link.target, 'width=500,height=600,resizable');
return w ? false : true; // if popup blocker, use the default behaviour of the link
}
</script>
Default.aspx.cs page
if (Session["PagesViewed"].ToString() == "5")
{
//Call my Javascript function How?????
}
You can output javascript into a LiteralControl from your code behind:
.aspx:
<asp:Literal id="myLiteral" runat="server" />
Code behind:
myLiteral.Text = "<script type='text/javascript'>popWin('url');</script>";
When rendered this way, the output script will call the function - make sure it is lower in the page than where the function was defined to ensure it exists.
In ASP.Net you can do the following:
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"openpopup",
"popWin('www.someurl.com');",
True);
If you need more control over your scripts placement #Oded has a better approach - as trying to call a function that has not been defined isn't a good idea...
You cannot call javascript functions directly from C#. However, what you could do is pass a <script> to the browser that executes the function.
response.Write("<script>popWin(something);</script>");
I am using [script.js][1] as async script loader along with my master page. The pages on the root are working fine using the master page file as it's master page. When it comes to web pages that are inside folders like below then the path does not work fine. Script loader loads the files from wrong url. How do i make it load from correct url regardless of the path.
Admin
-users
-createuser.aspx
The contents of the loader file
//show the path to load scripts files from
$script.path("/js/");
//load jquery first and then load the dependent scripts
$script.order(["jquery.min","slider"],function(){
//load the application specific file
$script("app");
});
how do i get the path to web application on client side for example path should resolve to
http://domainname/virtualdirectory/js/
Are you looking for somthing like this?
<%= Page.ResolveClientUrl("~/Scripts/test.min.js") %>
This will resolve the url for you from the HTML side. I am not familiar with $script.path, but I am wondering if you can do somthing like $script.path('<%=Page.ResolveClientUrl("~/Scripts/test.min.js") %>');
It seems like $script.path("~/js"); would be better. Also, please, write down the wrong path here, i'll try to guess why it is wrong
try this out:
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
string script;
script = "function getURL(which){ if(which=='1') { return '" + ResolveUrl("~/Default.aspx") + "'; } }";
ClientScript.RegisterClientScriptBlock(this.GetType(), "MyFunction", script, true);
}
JAVASCRIPT-With JQuery:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
alert(getURL('1'));
});
</script>
you will have to know which URL to load, but it works like a charm. Good luck!
The Master page got a ScriptManager.
Then i got a Control with a ScriptManagerProxy and an UpdatePanel.
Inside the UpdatePanel there I dynamically add a Control (also containing a ScriptManagerProxy) and from that control I need to run some JavaScript code.
DynamicControl.ascx:
<script type="text/javascript">
function doSomething() {
alert(1);
}
</script>
DynamicControl.ascx.cs:
public void Page_Load(object sender, EventArgs e)
{
...
ScriptManager.RegisterStartupScript(
this.Page, this.GetType(), "scriptID",
"<script type='text/javascript'>doSomething();</script>", false);
My problem is that the function "doSomething()" is never called and i dont know why. :S
Edit: It is called but not directly when i add the control.
If I do code like this, there will be an alertwindow:
"<script type='text/javascript'>alert(1);</script>"
Ok I think i need to add some more information:
The control is dynamical added inside a jQuery Dialog. And I found out that the javacode is first executed after i close and then open the dialog. Some kind of event is triggered so that the code is executed there i think. Is it possible to force this event? so the scripts are executet directly when the control is added ?
placeHolder.Controls.Add(dynamicReportControl);
This c# code doesn't execute the javascript tag immediately ?
Please try something like this
ScriptManager.RegisterStartupScript(
this.UpdatePanel1, this.UpdatePanel1.GetType(), "scriptID",
"<script type='text/javascript'>doSomething();</script>", false);
Where UpdatePanel1 is your UpdatePanel
Try setting the addScriptTags argument to true, and remove the script declaration from your code.
Is that code only supposed to run on every request? If so, why not just add this code to the ASCX:
window.onload = doSomething();
I'm sure this is fairly straightforward but i'm having trouble getting it to work. I want to add a javascript function to my page, but only when the page postsback.
So I have a button that calls some server-side code, and when it is finished and the page is re-loading I want the javascript function to be called.
Thinking about this i guess I could just add a hidden variable and set it when the button is clicked, but i think i'd rather just insert the javascript onto the page when it is loading back.
Is this possible, or even a good way to do it?
Thanks,
Neil
Edit: Okay this is my OnClick method in the C# code.
protected void Save(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script type=\"text/javascript\">alert('hello world');</script>");
EnforcementMatch(false);
EnforcementMatch(true);
ApplicationNotMatch();
ApplicationMatch();
Response.Redirect(Request.Url.ToString());
}
Another Edit: Just realised that the response.redirect at the bottom reloads my page cancelling out the code I put in, duh!
You can use ClientScriptManager.RegisterClientScriptBlock
http://msdn.microsoft.com/en-us/library/btf44dc9.aspx
If you place it on the button click event, you don't have to worry if it's a postback or not.
You know about the IsPostBack function, right?
IsPostBack (MSDN)
if (IsPostBack)
{
ClientScript.RegisterClientScriptBlock()
}
This script will be fired with every single postback from updatepanel
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function () {
alert('hello world');
});
</script>
Place it in a separate script block, only to be rendered on postback.
<script type="text/javascript" runat="server" visible="<%#this.IsPostBack %>">
TheCode();
</script>