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

Hey I have a bunch of JavaScript/Jquery code that works really well when I use a Content-Editor webpart, but does not execute when I add the same code to the VisualWebPart mark up.

I am referencing the JavaScript in the following way for both:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
//Code here
</script>

Any idea why my JQuery code is not being recognized? (Yes I am using the $(document).ready(function() { .. }) )

Note: The JQuery code is not loaded on the page

I am fairly new to SharePoint development, visual webparts in particular. If you could help out I'd truly appreciate it.

Thank You, MyName.


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {

        alert('test!'); 

        //move he last list item before the first item. The purpose of this is if the user clicks to slide left he will be able to see the last item.
        $('#carousel_ul li:first').before($('#carousel_ul li:last'));

... more Code


    });
</script>
share|improve this question
    
Can you at least get an alert out of it? –  Daniel Ziga Mar 12 '12 at 14:55
    
@Dandroid Unfortunately not –  MyName Mar 12 '12 at 15:00
    
May be another jquery version loaded before yours? and conflict appear... –  Kai Mar 12 '12 at 15:48
    
@Kai YES You're right, there was another JQuery version overriding mine in the Master Page o god it works now thank you so much –  MyName Mar 12 '12 at 16:00
    
I've posted it as answer, please mark it as accepted if it help –  Kai Mar 12 '12 at 16:02

4 Answers 4

up vote 2 down vote accepted

May be another jquery version loaded before yours? and conflict appear...

share|improve this answer
    
That was it, Thank you so much ! –  MyName Mar 12 '12 at 16:05

At least you should use the following approach instead of jquery ready function:

<script type="text/javascript"> 
function TheFunctionToDoTheWork() 
{
    // Do the work here
}

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

The document.ready in many cases fires too early in the SharePoint page cycle. In the example above you can see the SharePoint interface for "subscribing" to the page loaded event in the SharePoint point of view.

share|improve this answer
    
I'm sorry I don't quite understand. Instead of the Document.Ready use the code you have pasted & my code goes in 'ReplaceText' or 'Custom Message' ? Or simply use what you've pasted to test? –  MyName Mar 12 '12 at 15:09
    
Sorry I've pasted my own example from the other answer from the mobile device and wasn't be able to edit it. I've updated the answer and have removed unrelated details. –  Alex Boev Mar 12 '12 at 15:24
    
Its no problem, it didn't work, the same issue is present as before, but I'll use what the _spBodyOnLoadFunctionNames instead of the jQuery document.load –  MyName Mar 12 '12 at 15:36
    
Thank you so much –  MyName Mar 12 '12 at 16:01

try to use this:

<script type=\"text/javascript\"> window.jQuery || document.write('<script src=\"path_your_jquery\">

If your problem has anything to do with the Jquery conflict then this should solve the problem.

share|improve this answer

Using Firebug or I/E Developer toolbar, make sure that the jquery is being loaded on the page.

Next, make sure that your $(document).ready(); is not getting called before the jquery script loads.

Also, try the following code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
EnsureOrDelayUntilScriptLoaded(function(){
//Your Code here
},"jquery.min.js");
</script>
share|improve this answer
    
The JQuery is not being loaded on the page, I checked through Mozilla FireBug... Any ideas as to why this may be? –  MyName Mar 12 '12 at 15:07
    
can you access the jquery url from the browser? –  Vardhaman Deshpande Mar 12 '12 at 15:22
    
Yes sir, I can ~ –  MyName Mar 12 '12 at 15:23
    
how are you loading your script exactly from the visual webpart? can you post that code here? –  Vardhaman Deshpande Mar 12 '12 at 15:25
    
the way you would in a .NET Application, simply with the use of the script tags. Is there more to adding JavaScript & sure I'll edit the question to include the code –  MyName Mar 12 '12 at 15:31

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.