javascript - ASP.net: ClientScript.RegisterClientScriptBlock fires before jQuery is loaded - Stack Overflow

Interesting problem here from some inherited code I recently looked at. I'm trying to add a pressi

Interesting problem here from some inherited code I recently looked at. I'm trying to add a pression module to a project. It is loading all the JS and CSS files, bining them, minifying them, and pressing them. I've tried a number of solutions, but all of them have one fatal problem.

I have some javascript that is being loaded via Page.ClientScript.RegisterClientScriptBlock in the PreRender of the MasterPage. The pression module is loading as a Script Tag link in the MasterPage, but when I run the page... the code from the PreRender is lopped on top and is giving me a '$ is undefined' error, telling me jQuery isn't loaded yet.

Furthermore, I can't seem to get past the same problem when it es to inline javascript on content pages.

Any ideas as to what is causing this? Enlighten me as I have no clue.

Interesting problem here from some inherited code I recently looked at. I'm trying to add a pression module to a project. It is loading all the JS and CSS files, bining them, minifying them, and pressing them. I've tried a number of solutions, but all of them have one fatal problem.

I have some javascript that is being loaded via Page.ClientScript.RegisterClientScriptBlock in the PreRender of the MasterPage. The pression module is loading as a Script Tag link in the MasterPage, but when I run the page... the code from the PreRender is lopped on top and is giving me a '$ is undefined' error, telling me jQuery isn't loaded yet.

Furthermore, I can't seem to get past the same problem when it es to inline javascript on content pages.

Any ideas as to what is causing this? Enlighten me as I have no clue.

Share Improve this question asked Jan 21, 2011 at 14:55 jlrolinjlrolin 1,6249 gold badges38 silver badges68 bronze badges 1
  • jQuery isn't designed to work before the DOM is fully loaded. – Jake Commented Jan 21, 2011 at 15:04
Add a ment  | 

4 Answers 4

Reset to default 9

If have done this before with RegisterStartupScript (instead of RegisterClientScriptBlock) and called the $(document).ready(function() from WITHIN that script.

If the script tag link that eventually expands out to jquery is not in the head, but in the body of the page, then $ will be undefined when the script block executes, unless it is included in the html before the opening <form /> tag in the rendered html, which I understand is where RegisterClientScriptBlock spits out its script (just after that opening tag).

If this is not the case, and the joined/minified script is in the head, then I'd use a browser debugger such as Firebug or IE Dev Tools to verify that the jquery script is being correctly included in your bined script.

I know this answer is late to the party, but try calling ClientScript.RegisterClientScriptBlock in your OnPreRenderComplete (rather than OnPreRender) handler. This inserts the code later in the page rendering process.

All your jQuery code should be written inside the DOM-ready function:

$(function() {
    // your code here
});

indipendently from where you place it in the page, 'cause the jQuery() function isn't avalaible before.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743662076a4486363.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信