1

i have the following simple javascript function

function invokeMeMaster() {
    alert('I was invoked from page');
}

it works properly in the code but when i add it as an external js file noting happens. i try to add js file and call this function in a button click as this

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script  type="text/javascript">   src = <%= ResolveUrl("~/Scripts/JScript1.js")%> </script>

</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

   <asp:Button ID="btnMaster" runat="server" Text="Button"  OnClientClick="invokeMeMaster();"/>

</asp:Content>

what am i missing? the code does not work in this way? rhanks

1
  • Read the generated source and you will be enlightened. Commented Aug 15, 2013 at 14:14

5 Answers 5

2

src is an attribute of the <script> tag.

You're creating a Javascript variable and assigning it to invalid syntax.

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

Comments

2

Your script tag isn't correctly formed. Try:

<script  type="text/javascript" src='<%= ResolveUrl("~/Scripts/JScript1.js")%>' ></script>

2 Comments

I have try this, but nothing happens? please help ?!
Is your JScript1.js file definitely at the following directory: *root*/Scripts/JScript1.js? All the spelling correct? I get tripped up all the time by one rogue character...
0

Youcan try this:-

<script  type="text/javascript" src = <%= ResolveUrl("~/Scripts/JScript1.js")%>>    </script>

instead of

<script  type="text/javascript">   src = <%= ResolveUrl("~/Scripts/JScript1.js")%> </script>

src should be included in the script tag.

Comments

0

The code should look like this:

<script  type="text/javascript" src='<%= ResolveUrl("~/Scripts/JScript1.js")%>'> </script>

Comments

0

There are a few ways you can do this... I personally would give the button control a class (CssClass property) and target that with JavaScript.

Using jQuery, I would do:

(function () {
    function invokeMeMaster() {
        alert('I was invoked from page');
    }

    $('.btnMaster').click(function() {
        invokeMeMaster();
    });
});

I honestly wouldn't even use the the asp:button control. It expects to have a post back... I mean, there isn't anything wrong with using it per-se, but my approach would be different.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.