Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Editing Javascript in code

Posted on 2010-11-23
3
Medium Priority
?
485 Views
Last Modified: 2012-05-10
Good afternoon,
My ASP.net application writes some javascript depending on several options selected on a page, and then attaches it to a button, so onClick it fires the javascript.

The issue I have is its a pain clicking the 'Compile' button to attach the Javascript to my button, then having to click the button.

I have through about setting a javascript method to press the button for me 1 second after the page completes load, but it causes 2 problems, 1 I cant seem to guarantee what the button is called during run-time, and the second is the click just causes the page to post back again, and its stuck in a loop.

What I thought about doing is having a javascript segment on the page:-
         <script type="test/javascript" id="javaRunMe" runat="server">
         </script>

And then adding the javascript inside my code:-
         javaRunMe.innerTest = myJavaCode
But this didnt work either, just gave me an error about 'Object reference not set to an instance of an object'

I did also think about writing a Javascript method to see if Button2 had any attributes set, and if it did run them, but again I ran into the problems of I cant garantee what the buttons name is, and I couldnt find any way to test Button2 attributes.

Any ideas?????????
0
Comment
Question by:tonelm54
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
JosephEricDavis earned 2000 total points
ID: 34197492
So first of all, where is your custom javascript being created?  I'm guessing it is being created on the server right?  And then being placed onto the page using code like this below?

ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());

Or something similar?

If that is the case than you should just be able to put the jquery document completed function on the page and attach a click event with to the button your talking about so it calls your custom function...

So on the server...

StringBuilder sb = new StringBuilder();
sb.append("function yourCustomFunction() {");
sb.append("//whatever code you decided to add in here.");
sb.append("}");
ClientScript.RegisterStartupScript(this.GetType(), "CustomScript", sb.ToString());

Then on your client you can do something like this...

$(function() {
     $('#yourbutton').click(function() {
          yourCustomFunction();
     });
});

Where the html button in your page has its id set to "yourbutton"

Let me know if this is anywhere in the direction you're heading on this, or if you need any more help trying to piece together what I'm showing you here.
0
 

Author Comment

by:tonelm54
ID: 34203830
Well, actually JosephEricDavis, I havnt tried your way, but seems to make sence.

Ive tried adapting your code to suit my needs:-
       Dim sb As New StringBuilder
        sb.Append(loadcmd)

        ClientScript.RegisterStartupScript(Me.GetType, "JSScript", sb.ToString)
The idea is to just run the code once loaded, I would prefer not to have the user click a button.

But when it compiles it puts my code outside the script tags:-
<script type="text/javascript">
//<![CDATA[
(function() {var fn = function() {$get("MainContent_ToolkitScriptManager1_HiddenField").value = '';Sys.Application.remove_init(fn);};Sys.Application.add_init(fn);})();//]]>
</script>
window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Administration");window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Anti-Virus");window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Audits");<script type='text/javascript'>new Sys.WebForms.Menu({ element: 'NavigationMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false });</script>
<script type="text/javascript">
//<![CDATA[
Sys.Application.add_init(function() {
    $create(Sys.Extended.UI.CalendarBehavior, {"format":"yyyy-MM-dd","id":"MainContent_TextBox1_CalendarExtender"}, null, null, $get("MainContent_TextBox1"));
});
Sys.Application.add_init(function() {
    $create(Sys.Extended.UI.CalendarBehavior, {"format":"yyyy-MM-dd","id":"MainContent_TextBox2_CalendarExtender"}, null, null, $get("MainContent_TextBox2"));
});
//]]>
</script>


BTW- My code is the:-
window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Administration");window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Anti-Virus");window.open("viewChargeInvoice.aspx?startDate=2010-11-02&endDate=2010-11-15&Contract=Audits");

Any ideas what Im doing wrong?

Thank you
0
 
LVL 7

Expert Comment

by:JosephEricDavis
ID: 34206151
So I need more detail on what your currently doing and what you're trying to do.  

Are you dynamically creating the javascript on the server or is it being done on the client?

You mentioned that previously the user was having to click a button twice?  Now are you saying that you don't want the user to have to click a button at all and you would just like to have your code run as soon as the page loads?  Give me more of a description of what you need to have happen.

Tell me anything else you think might be helpful for me to understand your situation.  The more detail you provide me the easier it will be for me to provide a solution for you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question