Thursday, December 3, 2009

Asp.net Master page and JQuery reference – JS Inside Master Pages

LavaLamp for JQuery lovers ! “ nice name you will find it when you try to google for JQuery Menu :) http://www.gmarwaha.com/blog/2007/08/23/lavalamp-for-jquery-lovers/ try It! .

I download it and create the example page on just 3 min :) and take 6 hour to apply it to my master page. all you need to do to add the JQuery plugins is to add these lines to you Master Head tag  (add Jquery references and lavalamp )

<head runat="server"  style=" height:100%;"  >
    <title></title>
 <script type="text/javascript"  src="../Js/jquery-1.2.3.min.js"></script>
   <script type="text/javascript"  src="../Js/jquery.easing.min.js"></script>
   <script type="text/javascript"  src="../Js/jquery.lavalamp.min.js"></script>
   <script type="text/javascript" language="javascript">
        $(document).ready(function() {
            $(function() {
                      $("#1").lavaLamp({
                            fx: "backout",
                          speed: 700,
                           click: function(event, menuItem) {
                                 return false;
                            }
                          });        });        });
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
     </script>
</head>

Great you will get this error :

image 

This occurs of course since jQuery hasn't been loaded by the time the function tries to run.

Solution : 

1- Initially my script was placed below my head ContentPlaceHolder
2- If your MasterPage is not at the same directory level then things won't work either. You can use ResolveUrl
3-forces the header to resolve data binding expressions, and everything gets back to normal! by adding the below code to master page code behind

Header Code :

<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<script type="text/javascript"  src='<%# ResolveUrl ("~/Js/jquery-1.2.3.min.js") %>'></script>

Code Behind

 protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Page.Header.DataBind();
    }
hope my 6 hour of searching :) help someone avoid run into these problems.:) 

18 comments:

  1. this indeed save me a lot of time. thanks

    chris
    ReplyDelete
  2. thanks for sharing this! this save me a lot of time too.
    ReplyDelete
  3. Great ! thank you very much !
    I fixed my error.
    Leo
    ReplyDelete
  4. the master page not resolving the url was the problem. ugh...

    Even though I already spent my own time on it, and google time... Your solution is ultimately the solution.

    Only problem I had was
    base.OnLoad(e);
    I have to comment that line out as I get a stackoverflowexception error if not... Works without it, and long as i have the second line Page.Header.DataBind();

    Many Thanks!
    ReplyDelete
  5. Many many thanks. Very nicely explained with screenshots and clear solution steps. You made my weekend.

    5 Stars!
    ReplyDelete
  6. I was really happy to find this as it described my situation perfectly. However, even after doing everything you said, I am still getting the "object expected error". I am a real newbie. Can anybody help? Here is my code (right after the ContentPlaceHolder for "head"):



    $(function() {
    $('.example').truncate({ max_length: 24 });
    });


    "script" replace by "scropt" so I could post it.
    Thanks, Mark
    ReplyDelete
  7. I notice that the script lines didn't post. I am using the example exactly as instructed from:
    henrik.nyh.se/2008/02/jquery-html-truncate

    Thanks for any help, Mark
    ReplyDelete
  8. I want to bear your children! I've wrestled with this on and off on my last three projects - I've managed workarounds but this is the clean solution I've been looking for :)
    ReplyDelete
  9. I done what u suggest. Still facing same problem.

    Please help
    Thanks
    Kunal.pagariya@gmail.com
    ReplyDelete
  10. Thank you so much!!! Worked like a charm!!!
    ReplyDelete
  11. This link (http://forum.jquery.com/topic/superfish-not-working-in-master-page-get-object-expected-on-function) helped me fix this issue but this post was a great starting point. Thanks!
    ReplyDelete
  12. HeY Mohammed, Thanks a zillion you saved my day in fact days!! God bless u !
    ReplyDelete
  13. You could use something along the lines of :
    http://guyellisrocks.com/coding/microsoft-jscript-runtime-error-object-expected-with-jquery/
    ReplyDelete
  14. You are great :)
    ReplyDelete
  15. Thankyou so much ..its solved my problem http://www.taanu51.blogspot.com/
    ReplyDelete
  16. thanks. I hope hope i found this earlier as I am struggling with the referencing of jquery
    ReplyDelete