Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a sandboxed sharepoint project, in which I have created a module, put jQuery there and referenced it from my webpart's ascx page like this:

<head>
<script src="/Content/jquery-1.7.2.min.js" type="text/javascript"></script> 
</head>

The thing seems to load properly and the scripts work - the thing is when I'm setting some personalizable properties in the webpart's options, its supposed to fire a jquery script from codebehind, like this:

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        Controls.Add(
             new LiteralControl("<script language=\"javascript\" type=\"text/javascript\">jQuery(document).ready(setClockOffset($, " + ClockTimeZone9 + ",\"" + ClockTimeZone8 + "\"));</script>"));

    }

And this gives me the following error: Uncaught ReferenceError: jQuery is not defined It seems like LiteralControl adds the markup as the first element inside head, instead of possibly adding it after the jquery reference. What would be the best way to approach an issue like that?

share|improve this question
add comment

2 Answers

Ok, so the solution to this what seemed to be trivial problem was actually a bit longer than i would've expected.

first I added the jquery sctiptfile to a module "Content", and referenced it inside the webpart's Elements.xml file, like this:

<Elements>
....
<CustomAction
ScriptSrc="~SiteCollection/Content/jquery-1.7.2.min.js"
Location="ScriptLink"
Sequence="10">
</CustomAction>
</Elements>

Also all my javascript logic I have moved to a separate file and referenced it the same way. This way I could add whatever I wanted in the form of adding a LiteralControl and it worked. Obviously the javascript had to be a bit modified for this approach to work.

share|improve this answer
add comment

In my 365 projects, I created a folder called "Modules" inside the "Modules" folder I created another folder called "Scripts", I then put my Jquery and other scripts in this folder.

You can put the following in your code.

<script> window.jQuery || document.write('<script src="<%= SPContext.Current.Site.Url %>/path_your_module/Scripts/jquery-1.7.1.min.js"><\/script>'); </script>

Or you can create a class what inherits a WebPart and create an override method for OnInit. From there you can create a new literal control:

Controls.AddAt(0, new LiteralControl("<script type=\"text/javascript\"> window.jQuery || document.write('<script src=\"" + WebUrl + "/path_your_module/Scripts/jquery-1.7.1.min.js\"><\\/script>');</script>"));

WebUrl is a property with the url of your site.

Don't forget to inherit it in your webpart within the class you created!

share|improve this answer
add comment

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.