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

In my master page, I have attached jQuery along with my own custom.js. I use the javascript in that file to run fixes on my pages such as style adjustments.

Now, when I run simple functions on my sharepoint pages, they never run on the outputted markup unless I use _spBodyOnLoadFunctionNames.push on the function. Alerts WILL fire, however. So, I tried using jquery's document.ready instead, but it NEVER fires. Neither does ExecuteOrDelayUntilScriptLoaded ever fire.

For example, if I put this in my custom.js file:

$(document).ready(function(){
alert("bob");
}); 

It never fires. But if I just put

alert("bob"); 

It will fire just fine.

However, if I use _spBodyOnLoadFunctionNames.push to run my jquery functions, they DO work, so it isn't that jquery isn't getting loaded.

Am I missing something? Why won't document.ready work?

share|improve this question
2  
Sry, I just don't get the point why you don't want to use _spBodyOnLoadFunctionNames instead of document.ready? Apart from that, here's an article describing the reason of using document.ready or the sharepoint onload-function: stephanrocks.com/2011/10/05/… hope it helps. –  Dominik Dec 17 '12 at 8:56
    
(Sry, couldn't edit my previous comment): And have you tried to check your script(s) with firebug / Chrome developer tools? Their console would show errors for loading, referencing and script-logic errors. This is the first thing I do when adding javascript to... everything. Helps a lot ;) –  Dominik Dec 17 '12 at 9:03
    
Dominik, I wasn't trying to replace it, I just wanted to know why it didn't work (yes, I checked with Firebug). Thanks for the link to the article. –  BGM Dec 17 '12 at 15:53

3 Answers 3

up vote 5 down vote accepted

First it can be a conflict in jQuery. I always use "jQuery()" instead of "$()" with SharePoint and jQuery.noConflict and make sure there is only 1 version of jQuery being loaded.

Second the sequence of loading your script is important. Make sure it loads after jQuery is loaded. A good way to make sure is to load jQuery and your custom script using ScriptLink and load your custom script using attribute LoadAfterUI="true" this way jQuery will be loaded right away and your custom script after the UI is completely loaded.

share|improve this answer
1  
I placed the script calls in ScriptLink tags and it made the difference. I set jquery's LoadAfterUI to false and custom.js to true, now the document.ready works just fine even without the spBodyOnLoadFunctionNames call! Thank you! –  BGM Dec 17 '12 at 15:29
    
Ah, at the bottom of that page you linked to, there is this: var $1_7_2=jQuery.noConflict(true);, which allows you to define your own name for jquery, complete with noConflict built right in! Great! –  BGM Dec 17 '12 at 16:07
    
Hmm.. I thought that was a good idea about using the var $1_7_2=jQuery.noConflict(true); - but it seems to break a certain webpart I am using that uses 1.5.2 of jQuery in its own configuration. –  BGM Dec 18 '12 at 21:14

I have tried to create the situation you have and found the custom javascript is working fine.

I don't know how you are doing those things but I am putting here solution according to my guess.

If you are adding jQuery in HEAD secion of HTML then make sure your jQuery javascript file appears first and then your custom javascript file. Take a look at html code by viewing the page source in browser.

If everything is fine, then try to explain how you are adding the jQuery and cstom.js.

Amit

share|improve this answer
    
If I loaded my script before I loaded jquery, then my jquery functions wouldn't work at all, no? –  BGM Dec 17 '12 at 15:00
    
yes it would not work. Your jQuery file should be loaded first then you custom javascript file becuase $ is pointing to jQuery code. Also LoadAfterUI="true" can also fix if you are using ScriptLink to put the javascript reference in HEAD secion of HTML –  AmitKB Dec 18 '12 at 3:03

When using jQuery or general javascript for that matter, in a ASP.NET masterpage, it's very important that you know where to put it to make sure it takes effect.

If the javascript is placed in the header it is not always certain that the script will be read in time for the element to get affected by it. This can be solved by moving the javascript down beneath the HTML which it should have an effect on.

share|improve this answer

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.