Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Issue getting javascript to run. View modes: 
User avatar
Member
Member
vcarter - 4/15/2013 9:37:43 AM
   
Issue getting javascript to run.
Essentially the title says it all. I have added this to the top of my masterpage layout.
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
CMS.GlobalHelper.ScriptHelper.RegisterJQuery(this.Page);
}
</script>
<cms:CMSWebPartZone ZoneID="JavaScript" runat="server" />

My intent is to use the built in jQuery library and use a javascript web part to call a default .js file containing some basic functions.

That part, appears to be working(the web part), however I keep getting a:
"Uncaught ReferenceError: $jQuery is not defined"

So I figured I had referenced jquery incorrectly and switched the $jQuery back to just a $, problem is, the js reference is not being updated. And when I check the source it is still referencing $jquery. Is there some weird caching setting in Kentico? And I going about this all wrong.

External file reference seems to be the only thing in regards to Kentico that I have trouble with.

Any help on getting this to run smoothly would be appreciated.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 4/15/2013 10:55:19 AM
   
RE:Issue getting javascript to run.
I learned this the hard way as well. If you are using the built-in jQuery library use jQuery (vs. $jQuery) reference in place of $ in your custom jQuery files.

User avatar
Member
Member
vcarter - 4/15/2013 11:17:48 AM
   
RE:Issue getting javascript to run.
I switched it back to $jQuery. For some reason I am just not able to get my pages to use the core, at least I think that is what my problem is. From the documentation I figured:

I add the iquery registration code to the page(put mine at the top of my masterpage layout)

I changed the jquery reference from "$" to "$jQuery"

And that is where I get hung up. No matter how I seem to add my own JS file, be it a direct reference in the "head" section of the masterpage, or by using a javacript webpart, I get the:

Uncaught ReferenceError jQuery is not defined error.

Always breaking on my script pages
// DOCUMENT READY

$jQuery(document).ready(function(){


$jQuery("#menu-button").on("click",toggleMenu);

$jQuery("#header input[type='text']").on("focus",formrule_on);
$jQuery("#header input[type='text']").on("blur",formrule_off);

});

});

Document Ready call. It really feels like I am just missing something simple with the code and getting caught up in Kentico even though it may not be the issue.

Thanks again for the response.

User avatar
Member
Member
vcarter - 4/15/2013 11:18:56 AM
   
RE:Issue getting javascript to run.
LOL, I just reread your response, I missed the part where you dropped the $. Going to try that now.

User avatar
Member
Member
vcarter - 4/15/2013 11:43:07 AM
   
RE:Issue getting javascript to run.
Looks like my script is loading before jquery-core and that is causing my issue.

User avatar
Certified Developer 13
Certified Developer 13
kentico-jx2tech - 4/15/2013 12:01:24 PM
   
RE:Issue getting javascript to run.
In the javascript web part, try changing the page location from Header to Beginning of the page or Startup script.

User avatar
Member
Member
vcarter - 4/15/2013 12:07:45 PM
   
RE:Issue getting javascript to run.
Is the recommended method for adding additional jquery dependent scripts to use the javascript webpart as opposed to insertion direction into the templates head section?

User avatar
Certified Developer 13
Certified Developer 13
kentico-jx2tech - 4/15/2013 1:13:33 PM
   
RE:Issue getting javascript to run.
I am not sure sure if there is a recommended method. But considering that the RegisterJQuery() method will insert the jquery-core just below the <body>, you will have a conflicts with jquery script in the head.

Currently, I am using javascript web parts in the masterpage to include scripts. If it is jQuery, i check the Include jQuery property. I set the page location to Startup script unless there is a FOUC and then I use the Beginning of the page. Modernizr is the only script I place in the head.


User avatar
Member
Member
vcarter - 4/15/2013 1:38:03 PM
   
RE:Issue getting javascript to run.
Cool, I was just wondering if there was a best practice. Thanks for the reply.

User avatar
Certified Developer 8
Certified Developer 8
ashish.narnoli12-gmail - 5/15/2013 6:19:37 AM
   
RE:Issue getting javascript to run.
Hi,

You could try
var $jQuery = jQuery.noConflict();

$jQuery(document).ready(function() {
.
.
.
.
.
}

Thanks
Ashish Narnoli