-1

I have a javascript file file.js and I have a function FunctionfromJSpage() in that js page, first from onclick I was calling the function aspxPageFunction(); then from this function I need to call FunctionfromJSpage(); which is located in file.js.
I tried like this in default.aspx

<head>
  <script type="text/javascript" src="../page/file.js"></script>
  <script  type="text/javascript">
      function aspxPageFunction() { 
         //my code..
         FunctionfromJSpage(); 
      } 
  </script>
</head>

But I am unable to call that function.

6

3 Answers 3

3

harish,

I think what you're looking for is to add the function you've now defined (as Quentin pointed out) to an attribute of an existing asp control. For example:

<td>
    <asp:Button ID="Button2" runat="server" OnClientClick="YourFunction(); " Text="ClickMe"/>
</td>

You can do all sorts of things using this kind of methodology. It all depends on what you want to do. If you're trying to get something to happen when a user puts text in a textbox, you'll need to research the OnTextChanged event, dropdowns have the onchange event..perhaps a crash course in asp events might be useful to help you understand more about where to attach such things.

For client side (javascript events).

For server side - what we're talking about here - (asp events).

It looks like you're just starting to figure out the client server relationship and what you need to do to make things happen (I could be very wrong there though). If this is the case, I wish you well! I agree with the other posters - you should explore Google and this site a bit more, as these are VITAL resources to finding breakthrough on your coding endeavors.

-sf

Sign up to request clarification or add additional context in comments.

2 Comments

thanks sf for ur reply and suggessions, i tried these things bt it didnt worked well, the simple thing i need is to call a function located in a external javascript file from default.aspx page javascript.
That should be fine, you'll just need to make sure that your aspx page loads before the function call is made (or else your function won't be defined)
2

The code you have in your script element defines a function.

You say that you have defined it in file.js. To call it you just callFunction().

<script type="text/javascript" src="../page/file.js"></script>
<script  type="text/javascript">
    callFunction();
</script>

3 Comments

I know that this is super old, but I have a question - why can you not call the function in the first script tag that identifies the source page?
@Jake — By editing file.js? You can. The question was using a second script element though.
I see now that the original question has two script tags - thank you for your response!
0

Add the onload attribute to your body, this will call your function when the page loads <body onload="callFunction()">

or in codebehind

Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "scriptKey", "callFunction();",true);

Have a look at this great article http://www.codeproject.com/Articles/11098/Use-Call-RegisterStartUpScript-RegisterClientScrip

3 Comments

hi.. i want to call that javascript function from default.aspx page not from default.aspx.vb/.cs
When do you want the function to be called onload, onclick, when?
first from onclick i was calling the function "call();" then from this function i need to call "callFunction();" which is located in "file.js". i hope u understood my requirement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.