0

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>
4
  • Can you at least get an alert out of it?
    – user2536
    Commented Mar 12, 2012 at 14:55
  • @Dandroid Unfortunately not
    – MyName
    Commented Mar 12, 2012 at 15:00
  • May be another jquery version loaded before yours? and conflict appear... Commented Mar 12, 2012 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
    Commented Mar 12, 2012 at 16:00

4 Answers 4

2

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

1
  • That was it, Thank you so much !
    – MyName
    Commented Mar 12, 2012 at 16:05
4

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.

3
  • 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
    Commented Mar 12, 2012 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
    Commented Mar 12, 2012 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
    Commented Mar 12, 2012 at 15:36
2

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.

1

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>
5
  • The JQuery is not being loaded on the page, I checked through Mozilla FireBug... Any ideas as to why this may be?
    – MyName
    Commented Mar 12, 2012 at 15:07
  • can you access the jquery url from the browser? Commented Mar 12, 2012 at 15:22
  • Yes sir, I can ~
    – MyName
    Commented Mar 12, 2012 at 15:23
  • how are you loading your script exactly from the visual webpart? can you post that code here? Commented Mar 12, 2012 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
    Commented Mar 12, 2012 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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