jQuery unassign itself after loading - javascript

We have a CS-Cart store that we recently moved the directories on. After the move, we have found that the Javascript on the home page is not working correctly. A number of $(document).ready() events aren't working correctly and are throwing a TypeError: $ is not a function error.
After stepping through the concatenated file, we have narrowed the problem down. The file is processed correctly but after the first $(document).ready() event is called, jQuery seems to unassign itself. IE both the jQuery and $ variables are unset.
We have access to a wayback machine copy of the site and that seems to work properly. If we copy the old javascript file across though, we get the same error. This fact seems to suggest that it is some other mechanism that is causing the problem.
Is there a known mechanism that CS-Cart or jQuery will unassign the jQuery variables?

So it turns out that the MailChimp embed code was old and the jQuery noConflict method was causing the issues. Once we turned the embed code off, everything worked as expected.

Related

Wordpress 4.8 - ReferenceError: Can't find variable: wp

After updating to Wordpress 4.8 widgets stop working, I mean the textarea inside has disappeared and I read the following error in console:
ReferenceError: Can't find variable: wp
Global Code — widgets.php:202
Looking into the file I see the line causing the issue:
wp.mediaWidgets.init();
and so on. There are other blocks related to the wp variable but I really cannot understand why it's happening considering that I've already tried to disable all plugins without success. Any advice?
Thanks.
How did you update to 4.8? You might try manually updating WordPress. Your issue could be from inconsistent versions of some files. Source
Check out these instructions.
If this doesn't work, can you say which widgets.php file is causing the problem? I noticed there were a few files with that name in the codebase.
I actually solved manually restoring the Wordpress core and cleaning the wp-config.php. Furthermore there was a function in my theme deferring javascript and that was the main cause, most likely.

Jquery Cookie plugin doesn't load in wordpress

I've got a wordpress site with jquery cookie plugin installed. It used to work fine. For a little while, i've been working with a content that didn't require the plugin. Meanwhile, i've started using cloudflare.
Today, I tried adding content that utilizes that plugin, and suddenly got an error saying "Uncaught TypeError: $.cookie is not a function".
Obviously, the plugin is not loading. What i've done first is, i've moved that plugin from a separate file into header. Right above the script that utilizes it. No luck. I figured, it could still be showing cloudflare cached version, so, i've deleted cache and minified scripts & css. Still no luck. And now I'm stuck.
I know that people here like some code to be provided, but as you can understand, there is a lot of it.
Is it possible that $.cookie is being called before it is loaded? Or that CloudFlare force the loading of a jQuery asset whose version isn't compatible with your cookie plugin?
Looks like disabling cloud flare took a while. Works fine now.

jQuery UI accordion working in MVC 4 debug mode but not in the release

I work on an ASP.NET MVC 4 application.
I updated jQuery, jQuery UI and all other nuget packages in my project. Built it and ran it to see that no problems occured. Everything worked so I continiued development for several days.
After a while I published the project to a test-server I have to see how it looked on Ipad, Iphone etc. And then I realized that the jQuery-UI accordion stopped working.
It renders, but does not respond on click. This goes for all browsers. I found this strange since the only difference is that the MVC bundeling i applied in release mode and as far as I understand it either minifies the jQuery file or chooses the minified version (the latter probably beeing what is does here since it is added when updating using nuget).
The jQuery version is: 2.0.3
The jQuery-ui version is: 1.10.3
In Chrome console the error reads:
Uncaught TypeError: Cannot read property 'safari' of undefined
OK, so i realize this might be the result of one of my jQuery assemblies references $.browser that apparently was removed in jQuery 1.9.
However the fact that it works in debug mode (using the non-minified versions) leads me to think that it might not be that. I could almost think that it is a problem with the minified versions, but that should not be possible either.
Anyone knows why this happens and know how to fix it?
Update
As requested I attach most of the code. The strange thing is that this works in debug mode, but not in release. I cannot see why there would be any significant changes that should cause this problem:
_Layout.cshtml
Bundleconfig.cs
The view (which calls a partial view)
The partial view
The jquery code
The javascript error in Chrome
Bigger view of the error in chrome
Just to add. After posting this I removed the duplicate loading of modernizr (the telerik custom one was added without me noticing). The problem still persists.
Update 2
I turned off the optimizations in bundleconfig.
BundleTable.EnableOptimizations = false;
This allowed me to see what was throwing an error in jquery-ui, and it seems indeed to be the $.browser that was removed. It seems that it actually loads two old versions of jquery (current + 1.6.4 and 1.7.1) together with the new one and two versions of jquery-ui (current + 1.8.16) the last one throwing an error. None of these files are loaded in my solution or in the file folder.
This seems to be a publish problem, but disguised by the bundling and minification... I'll try to fix this and see if it works.
OK, so this was not at all a problem with the code itself, and I learned something new that might be of help to others.
Setting
BundleTable.EnableOptimizations = false;
Was what helped me solve this.
Obviously the Asp.Net MVC 4 bundling bundles every script that is found in the folder where it is released, not what is in the solution folder in Visual Studio. I did not think of this at all... But in retrospect it kind of makes sense.
Combined with the fact that VS default publishing settings does not remove files that does not equal the files that you publish means that when removing i.e. an old version of Jquery and adding a new one, the old one still remains on the server.
Add the default bundling using {version} for jquery selection and you have one unreadable jquery file throwing error. Not until you disable bundling and minification it becomes obvious that I in this circumstance loaded 3 versions of Jquery:
Before
After
The solution? Easy, just have to expand a setting called "File publish options" in the publish wizard and check "Remove additional files...":
Thanks for the suggestions. It helped me look the right places, but I was not able to imagine this beeing the problem.
I think you have forgot to add '"~/Content/themes/base/jquery-ui.all.css"'
Note: although you have called all modules separately sometimes calling single file makes difference. As we might miss some dependent file in choosing separate modules.
In BundleConfig.cs you need to add/modify like..
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery-ui.css",
"~/Content/themes/base/jquery-ui.all.css"
));
if in your locals the file of jQueryUI exist that will do the trick for the same(that might be possible problem that makes it working on Local Testing).
Your _layout.cs will be like..
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryui")
#Styles.Render("~/Content/themes/base/css")
#RenderSection("scripts", required: false)
as you are not rendering jqueryUI that we need to call and render saperately after jQuery.
And your Section script should be called like this,
#section Scripts{
#*#Scripts.Render("~/bundles/jqueryUI")*#
<script type="text/javascript">
$(document).ready(function () {
$("#accordian").accordian();
});
}
This is what I will suggest and should work, with MVC things are really small to identify exact problem, So I have provided all these lines, thank you,
Do post back if problem persist.

Tinymce won't initialize on Safari

I have a tinymce editor in my application and everything works fine.
However, I discovered a bug on Safari.
Here's what happens:
I open the page once and everything works fin. I go to another page and then go back to the previous one. Now the tinymce isn't initialized. Instead, I see just a textarea, and these errors in console:
TypeError: 'undefined' is not an object (evaluating 'f.document.selection')
TypeError: 'undefined' is not an object (evaluating 'j.createRange')
which happen in the tiny_mce.js file.
I found another user which had this problem:
http://lists.apple.com/archives/web-dev/2005/Jun/msg00146.html
But there isn't a solution provided. I'm stuck with this, does anyone know what the problem is?
Based on the error you're getting the tinymce lib isn't loading the moment you're calling the init fn. I highly speculate that your browser is loading the page from the cache (bfCache) which stops the event onLoad from running, in which you're script might be dependent on running that event first.
btw, I am working on a web application that operates heavily on tinymce v4 and it works very well in all browsers (Desktop and mobiles) so the bug your facing might be either a caching problem or a reordering the fns your calling. I would recommend you provide a reduced test case on Fiddle to debug that.

External js file problems

I have an external js file with some simple functions. They work fine when in the aspx page. When they are in the external js file, I get an error on the dev machine that the function is not defined. After playing with this for two days in IE9, I tried it in FF and it worked fine. Then, it worked in IE9 too. I had a second site with the same problem. I checked it again and it didn't work in IE9, then checked it in FF, it worked, and then it continued to work in IE9. What could possible cause this?
Ultimately this depends on whether or not the file is being included in the page correctly via the <script> tag. I'd be surprised if one browser can resolve a bad reference and another one cannot, since it's ultimately the server's job to determine if a file path is correct or not. So check that your include mechanism is always including the file correctly. You can run a trace with Fiddler for example, and watch for any 404 errors when the browser requests scripts.
The other possibility is that the script itself is being included correctly, but it contains syntax or references that Firefox accepts as valid but IE9 doesn't. For example, a reference to console will bomb in some versions of IE but will work fine in Chrome. Check that your script is valid to begin with.

Categories