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

Arian
  • 12,793
  • 66
  • 176
  • 300

2 Answers2

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.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I add reference to jQuery so endWith must recognize – Arian Aug 27 '11 at 08: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 '11 at 08: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.

AUSteve
  • 3,238
  • 21
  • 36