1

when I write this code:

<script type="text/javascript">
        function showhide(master, detail) {
            var src = $(master).children()[0].src;
            if (src.endsWith("plus.png"))
                src = src.replace('plus.png', 'minus.png');
            else
                src = src.replace('minus.png', 'plus.png');

            $(master).children()[0].src = src;

            $(detail).slideToggle("normal");
        }
</script>

and reference jQuery library in ScriptManager:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/scripts/jquery-1.4.1.min.js" ScriptMode="Release" />
    </Scripts>
</asp:ScriptManager>

every thing is ok.but when I commnet above code and reference jQuery In head part:

<script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

debugger alert me that for endsWith : Object doesn't support this property or method

why asp.net has this behaivior?

thanks

2 Answers 2

2

In this line of your code:

if (src.endsWith("plus.png"))

You are assuming that the string object has a method called endsWith(), but standard javascript does not have such a method. There are various extensions that add that method to the string object (it's a simple one to write), but you cannot assume it is there unless you've specifically added it to the String object prototype.

I can't explain why it ever works unless one of the two configurations has a library that installs this extension. It is not part of jQuery.

Here's an earlier SO question about adding an endsWith() method: endsWith in JavaScript and I happen to like this particular post in that discussion: endsWith in JavaScript.

2
  • I add reference to jQuery so endWith must recognize
    – DooDoo
    Aug 27, 2011 at 8:34
  • 1
    @Nima - Huh? jQuery does not have an endsWith() method. The problem with your code is that endsWith() is not defined. That is exactly what the error message is telling you.
    – jfriend00
    Aug 27, 2011 at 8:39
1

Check that the path to jquery is correct. The ScriptReference path is "~/scripts/jquery" (relative to the application root) but your direct reference is "scripts/jquery" (relative to the page) and these paths may be different depending on where the page is located.

2
  • no it's correct.I drag and drop my script on head section and above reference dynamically created
    – DooDoo
    Aug 27, 2011 at 8:15
  • Using drag-and-drop doesn't guarantee the reference will be correct at runtime.
    – AUSteve
    Aug 29, 2011 at 2:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.