trant trum
trant trum

Reputation: 219

How to place this java script function in an external file for user control?

I have a user control called header.aspx. In my user control if I do this it will work.

<script type="text/javascript">
    function greeting(){
        Alert("hi");
    }
</script>

 <asp:button id="button1" OnClientClick="greeting" /> </asp:button>

I am using this user control in a page called default.aspx. I tried using src="~scripts/foo.js". If I do that it does not work. The question is pretty simple I guess. How would I call a java script function in a user control which is stored in an external location( not in the page. Located in the scripts folder). Thanks in advance.

Upvotes: 0

Views: 739

Answers (6)

Shruti
Shruti

Reputation: 1

External javascript file reference:

<script src="yourpath/yourfilename.js"></script>

Button Control

<asp:Button ID="button1" OnClientClick="greeting();" runat="server" Text="click" />

Upvotes: 0

bumbumpaw
bumbumpaw

Reputation: 2528

 <asp:button id="button1" OnClick=" javascript : greeting();" /> </asp:button>

try to use it. havent trie but i think it should work.

Upvotes: 1

Raja Danish
Raja Danish

Reputation: 235

First of all you need to create a seprate javascript file and then add this in your page in this way. add this tag in the tag of your page.

use

OnClientClick="greeting()"

you missed "()" in OnClientClick="greeting"

Please see the whole html code:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="scripts/foo.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:button id="button1" runat="server" OnClientClick="greeting()" Text="hit" />
    </div>
    </form>
</body>
</html>

Thanks.

Upvotes: 0

Krunal Patil
Krunal Patil

Reputation: 3676

As I can understand this is clearly a path issue. Just follow these steps might help you.

  1. Create a .js file first. Put your code and save it in the folder you want it to.
  2. Now Drag and Drop the js file inside the head section of your html code from the Solution Explorer window. This will give you the correct path for the js file.

The above steps is what I follow, when I create an external js file for my controls.

Also make sure you call your function in this manner also suggested by others Else your function won't get call:

<asp:button id="button1" OnClientClick="greeting();" /> </asp:button>

Upvotes: 2

Mohit
Mohit

Reputation: 81

Script: test.js

    function greeting() {
    alert("hi");
    return false;
}

user control: <asp:Button ID="button1" OnClientClick="return greeting()" runat="server" Text="click" />

Page:

    <head runat="server">
    <title></title>
    <script src="test.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:temp ID="temp" runat="server" />
    </div>
    </form>
</body>
</html>

This should work now.

Upvotes: 2

Ali
Ali

Reputation: 2592

Just use the code below:

<script src="<%: ResolveUrl("~/Scripts/foo.js") %>"></script>

Upvotes: 2

Related Questions