moment() function works in html but not in separate javascript file - javascript

I have included moment.min.js in my page
<script src="js/jquery.js"></script>
<script src="js/modernizr.js"></script>
<script src="js/moment.min.js"></script>
<script src="js/fatcalc.js"></script>
I can call
<script>document.write(moment());</script>
and it displays the date fine on my page.
But, when I call it from within fatcalc.js
var date = moment();
I get the error:
'moment' is not defined.
Why can my html page see it, but not the fatcalc.js file?

Well, I figured it out. Seems to be some issue with Jshint I don't understand but adding this to the top of my script fixed it.
/*global moment:true */

Related

Script stopped working without any recent code changes on the site

I have inherited a site and don't really understand the set-up!
The scripts on the site were all working ok then we are suddenly getting script errors and the script that entered a date in a field has stopped working.
this is at the top of the page:
<script type="text/javascript">
setURLs(//domainname/base/administration/);
</script>
I am getting this error: Unexpected end of input - what do I need to do to fix that?
Then this script:
<script>DateInput('Payment_Date', true, 'yyyy-mm-dd','2022-07-20')</script>
is giving the error DateInput is not defined and the field is not been added to the form - how do I sort that?
from the two lines of code that you shared I noticed a problem with
<script type="text/javascript">
setURLs(//domainname/base/administration/);
</script>
the // means comment so in order to make the function work you need to add some ' around it
like this
<script type="text/javascript">
setURLs('//domainname/base/administration/');
</script>

Error in Success callbackId: GeolocationXXXX : ReferenceError: Can't find variable: $ [duplicate]

I am using jQuery. This is my coding on my main page:
<script type="text/javascript" src="script.js">
</script>
and my script.js is:
$(document).ready(function(){
$("#title").click(function () {
alert("Works!");
});
});
My full coding can be found here: http://pastie.org/8676656.
Using a tool on the browser, I found an error in my javascript code:
ReferenceError: Can't find variable: $
on line:
$(document).ready(function() {
Any help would be appreciated.
You have to import jQuery before using it:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
Notice it is using // as protocol (not http:// or https://), it means: if your .html file is at a http:// server, it will get jQuery from http://ajax.google..., and if it is at a https:// server, it will get it from https://ajax.google....
Note: If, while developing, you open your HTML file in your browser instead of in a server, you should specify the protocol, as in this answer, otherwise it won't work:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Also, you should, if possible, place your .js files at the bottom of the page, right before closing </body>. See more in here.
Import jQuery before your code
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"><script>
Include jQuery before your script
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js></script>
this is jquery load problem,
load jquery before all your code and script.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js" ></script>

setDefaults function for JQuery UI not called correctly

I want a page to display the datepicker in french or Italian. To do so i import the datepicker and set it's default.
This is the imports :
...
<script src="/assets/jquery.ui.datepicker.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.ui.datepicker-it.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.ui.datepicker-fr.js?body=1" type="text/javascript"></script>
...
And in the body there is the following code:
<script type="text/javascript">
//<![CDATA[
$.datepicker.setDefaults($.datepicker.regional['it'])
//]]>
</script>
Despite it, the datepicker is in french.
However, if I type $.datepicker.setDefaults($.datepicker.regional['it']) in the JS console of firefox, my datepicker is now in italian.
I think the code is called because if i put a stop on it in the debugger, it is executed.
Why does the code embedded in the page is not called at the right time?

prettyPhoto Not Working - Error: Not a Function

I'm having some issues with prettyPhoto on one of my clients website. Here is the link for reference: http://www.browardmicrofilm.com/pages/kodak-vizit-essential.html
I've used prettyPhoto on multiple other websites without issue. However for some reason, this website just doesn't want to perform the script properly. Instead of opening an image in the lightbox clone, it simply opens it in a new page. Perhaps it has something to do with the hosting but either way, wanted to see what professionals like you think!
I'm using Firefox 26 (Mac version) and I used Firebug to determine the error:
TypeError: $ is not a function
$(document).ready(function(){
I've tried numerous solutions, including one that made me change "$" to "window.jQuery and then for some reason the next line in the code creates the same error.
Here's the my code for those of you that wish to skip the entire page source code:
In my header:
<link href="../prettyPhoto.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../Scripts/jquery.prettyPhoto.js"></script>
<script type="text/javascript" src="../Scripts/jquery-1.6.1.min.js"></script>
The final script just before the closing body tag:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$("a[rel^='prettyPhoto']").prettyPhoto({
theme: 'light_rounded',
});
});
</script>
I know my links are good, which is why I'm not including them.
Vanilla jquery needs to be declared before any library built on top of it
<!--first, jquery-->
<script type="text/javascript" src="../Scripts/jquery-1.6.1.min.js"></script>
<!--then the rest-->
<script type="text/javascript" src="../Scripts/jquery.prettyPhoto.js"></script>
Error below is saying that jQuery is not loaded.
TypeError: $ is not a function
$(document).ready(function(){
Check your resources, my guess is that your paths are case sensitive and libraries are not being properly loaded.
<script type="text/javascript" src="../Scripts/jquery.prettyPhoto.js"></script>
<script type="text/javascript" src="../Scripts/jquery-1.6.1.min.js"></script>
Check your resources.
Make sure that jQuery is loaded before the plugin is loaded.
<script type="text/javascript" src="../Scripts/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery.prettyPhoto.js"></script>
Ultimately I abandoned the prettyPhoto javascript and went with an alternative. Lightbox 2.0:
http://lokeshdhakar.com/projects/lightbox2/
It works just as I'd like and no problems.

I can't get the jQuery datepicker to show on my page after loading with AJAX

My main page is index.php. I'm loading a section of the page with AJAX and I would like to also load a datepicker in the same section I got all relevant JS:
<script type="text/javascript" src="javascripts/jquery/jquery.js"></script>
<script type="text/javascript" src="javascripts/datepicker/jquery.datePicker.js"></script>
<script type="text/javascript" src="javascripts/datepicker/date.js"></script>
I hope i'm not missing anything, the CSS is copied into my main css file.
and here's how the ajax looks like, at least the callback part:
xmlhttp12.onreadystatechange=function() {
if (xmlhttp12.readyState == 4) {
if(document.getElementById("corpcrm").innerHTML=raspuns[0]){
$(function()
{
$('#datepicker')
.datePicker({inline:true})
});
Now i know there aren't any problems with the the rest of the code as i've been using the very same thing for a while now, somehow I can't get the jQuery part right.
i tried adding a div directly to index.html and adding
<script type="text/javascript">
$(document).ready(function(){
$("#test").datepicker();
});
</script>
in the head. Firebug finds an error related to it that says
$('#test').datepicker() is not a function
The problem is now solved. I wasn't loading jquery-ui properly, was missing a > at the end of a script, thanks for the help guys.

Categories

Resources