Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Similar to some previous questions, I'm trying to use jQuery in a visual web part but I keep getting that jQuery is undefined. I've added the jQuery script to SiteAssets as well as a script folder inside my visual webpart project, and tried referencing it in the ascx file with both:

<script src="~sitecollection/SiteAssets/jquery-1.9.1.min.js"></script>
<script src="./Scripts/jquery-1.9.1.min.js"></script>

And also in code behind, in the Page_Load method, with lines such as:

this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "custJquery1", "~/SiteAssets/jquery-1.9.1.min.js");
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "custJquery2", "~sitecollection/SiteAssets/jquery-1.9.1.min.js");
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "custJquery2", "/SiteAssets/jquery-1.9.1.min.js");

I've also tried not referencing jQuery anywhere because I thought maybe the page (Discussion board topics) I'm using the webpart may already have jQuery on it.

The javascript I am using to check the jquery in my ascx file is:

<script type="text/javascript">
    function TheFunctionToDoTheWork() {
        if (typeof jQuery == 'undefined') {
            alert('jQuery didnt load');
        }
        else
            alert("Ive never received this message");
    }

    // "Subscribe to the SharePoint "page load"
    _spBodyOnLoadFunctionNames.push('TheFunctionToDoTheWork');
</script>

All I ever get is the alert that 'jQuery didnt load'. What might be wrong?

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

up vote 3 down vote accepted

I think the problem is src attribute of tag. Try <script src="/StyleLibrary/js/jquery-1.9.1.min.js" /> and take a look at this article

Edit If you decide to store js in _layouts folder, use <SharePoint:ScriptLink runat="server" ID="ScriptLinkJQuery" name="/Js/jquery-1.9.1.min.js" Localizable="false" />

share|improve this answer
How simple was that! Thanks – Steve May 21 at 5:38
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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