3

I need to make an application page or a webpart that have a javascript code and i tried to add the jquery i a new project to upload it to the SharePoint but the code didn't work this is the code that i am tried to run the same Visual Studio project

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead"
runat="server">
<script type="text/javascript">

$(document).ready(function () {
$('ul#people > li').css('background-color', '#ADD8E6');
});
</script>
</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<div id="myId">
<p>
Paragraph before List</p>
<ul id="people">
<li>Phill</li>
<li>Pip</li>
<li>Les</li>
<li>Denise</li>
<li>Martin</li>
<li>Helen</li>
<li></li>
</ul>
<p>
Paragraph after List</p>
<div>
<p>
Paragraph inside another div</p>
</div>
</div>
</asp:Content>
2

You would need to:

  1. Make sure you got jQuery properly loaded. Falak's approach absolutely valid, is but one of the most quicker - you need to make sure you upload your jQuery library in the _layouts/js folder prior to testing. There are other ways to load JQUery, such as using the ScriptLink (see here http://tommdaly.wordpress.com/2010/12/16/scriptlink-vs-whats-the-difference/) or a Custom Actions (see here http://weblogs.asp.net/jan/archive/2010/03/01/scriptsrc-referencing-javascript-files-with-sharepoint-2010-custom-actions.aspx)

  2. Check by code if jQuery is really loaded using either

    if (jQuery) {  
    // jQuery is loaded  
    } else  { // jQuery is not loaded }
    

    //or use this if (typeof jQuery == 'undefined') { // jQuery is not loaded
    } else { // jQuery is loaded }

  • 1
    There are many elegant approaches where you can add controls/scripts to pages without modifying the master page, DelegateControls, ScriptLink etc., but it's up to you to decide which suits your application best. – Falak Mahmood Jun 16 '12 at 20:46
1

First things first, let’s make sure if you are loading a copy of the jQuery script in the application page. For e.g.

<script language="javascript" src="http://yoursite/_layouts/js/jquery-1.7.1.js" type="text/javascript"></script>

  • should i have to upload it in the same application page ?? – sarah Jun 16 '12 at 14:14
  • 1
    deploying the jQuery Script File to Layouts can easily be done via VS2010. In your Right-click your project and add a SharePoint "Layouts" Mapped Folder. Include the jQuery file and you're good to go – Falak Mahmood Jun 16 '12 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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