jquery working but syntax error - javascript

$(function()
{
$(".error").hide();
$("#submit").click(function()
{
//form validate
var name= $("input#fullname").val();
if (name == "")
{
$("label#name_error").show();
$("input#fullname").focus();
return false;
}
var email= $("input#email").val();
if (email == "")
{
$("label#email_error").show();
$("input#email").focus();
return false;
}
var subject= $("input#subject").val();
if (subject == "")
{
$("label#subject_error").show();
$("input#subject").focus();
return false;
}
var textarea= $("textarea#textarea").val();
if (textarea == "")
{
$("label#textarea_error").show();
$("textarea#textarea").focus();
return false;
}
var dataString = 'fullname='+name+'&email='+email+'&subject='+subject+'&textarea='+textarea;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "form.php",
data: dataString,
success: function()
{
$("#form").html("<div id='message'></div>");
$("#message").html("<h2 id='success'>Query Submitted!</h2>").append("<p>You will be contacted shortly...</p><p>Reload the page to submit another query.</p>").hide().fadeIn(1500)
}
})
return false;
});
});
its just a small question
I have this website which has been working perfectly for the past 2-3 months ... but today when I accidentally wandered into the resource section of chrome's console window there was an error saying
"uncaught reference error: $ is not defined"
I haven't found a satisfactory answer on the web anywhere ... your thoughts?
here is the head section... i have not included any scripts anywhere else on the page
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="cq.css" type="text/css">
<script type="text/javascript" src="form.js"></script>
<link rel="shortcut icon" href="images/favicon.ico" />
<script src="jquery.js"></script>
<script src="form.js"></script>
<!--share-->
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript">stLight.options({publisher: "ur-c6d56dfc-f929-5bbf-a456-178fc403ae45"});</script>
<!--share ends here-->
</head>

You're calling your script before referencing jQuery. Change your HTML to:
<script src="jquery.js"></script>
<script type="text/javascript" src="form.js"></script>
You're also importing the same JavaScript file (form.js) twice.

Related

Uncaught TypeError: $.growl is not a function

I keep getting a "Uncaught TypeError: $.growl is not a function" error. Here is how I defined the Growl call and it shows how I am referencing Growl. What am I missing here? How can I fix this error?
index.php
<head>
<title>HOST Continuous Integration</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="stylesheet" type="text/css" href="HCIstyle.css">
<link rel="shortcut icon" href="Images/wci_favicon.ico">
<link href='http://fonts.googleapis.com/css?family=Armata' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Graduate' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:400,900' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Audiowide' rel='stylesheet' type='text/css'>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js' type="text/javascript"></script>
<script src='http://ksylvest.github.io/jquery-growl/javascripts/jquery.growl.js' type='text/javascript'></script> 
<link href="http://ksylvest.github.io/jquery-growl/stylesheets/jquery.growl.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.11.1.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<style type="text/css">
.table_outer { height: 15em; overflow: auto; }
</style>
</head>
script.js is the file where I am calling the Growl function:
// This function is called when, in the Submit Tab,
// the "Check Gerrits" button is clicked.
$("#check_gerrits_button").click(function(event) {
event.preventDefault();
if ($("#gerrits").val() == "") {
alert("Please enter gerrits.");
} else {
$("#main_form_su_validation_table").empty();
var data = { 'product_lines' : [], 'gerrits' : [], 'contacts' : [],'component' : ($("#component").val())};
//find all pl's that are checked
$("input[name=product_line]:checked").each(function() {
data['product_lines'].push($(this).val());
});
data['gerrits'] = ($("#gerrits").val()).split(",");
data['contacts'] = ($("#contacts").val()).split(",");
console.log("in chck gerrits");
console.log(data);
submitted = 'False';
$.ajax({
dataType: "json",
type: "POST",
url: "getsubmittedgerritsforSI.php",
data: data,
error: function (xhr, ajaxOptions, thrownError,response) {
// send the error mail -TBD
console.log(thrownError);
console.log(xhr);
alert(xhr.status);
alert(thrownError);
},
success : function(response){
console.log("get gerrits sucess");
console.log(response);
var data_cp = [];
var submittedlist = [];
$("input[name=product_line]:checked").each(function() {
for (var si in response) {
console.log(response[si]);
for (var i = 0; i < response[si].length; i++) {
gerrit = response[si][i]
data_cp.push(gerrit);
}
}
});
console.log(data_cp);
var ui_gerrits = ($("#gerrits").val()).split(",");
// this loop is to get the intersection of ui_gerrits and data_cp(database)
for (var i = 0; i < ui_gerrits.length; ) {
for (var j = 0; j < data_cp.length; ) {
if (ui_gerrits[i] == data_cp[j] ){
submittedlist.push(ui_gerrits[i]);
submitted = 'True';
}
j++;
}
i++;
}
console.log(submittedlist);
if (submitted = 'True' && submittedlist.length == ui_gerrits.length ){
//alert(str.fontcolor( "blue" ));
//$(function() { $.growl({ title: "Growl", message: "errits already released in SU or Submitted for SU!" })});
//$.growl(submittedlist + ": gerrits already released in SU or Submitted for SU");
$(function() {
$.growl({
title: "Growl",
message: "Gerrits already released in SU or Submitted for SU!"
});
});
}else if ((submitted = 'True') && (submittedlist.length != 0) ){
//alert(str.fontcolor( "blue" ));
//$.growl(submittedlist + ": gerrits already released in SU or Submitted for SU \nPlease remove " + submittedlist + " and resubmit remaining gerrits");
$(function() {
$.growl({
title: "Growl",
message: "Gerrits already released in SU or Submitted for SU!"
});
});
}
else{
SUValidation(data, '#main_form_su_validation_table', '#gerrits', "main_form");
}
}
});
}
});
});
You are missing the jQuery library. jQuery growl plugin is dependent on jQuery library. You can include jQuery library from a CDN like this or include it locally. But make sure to include it before the jquery growl library.
Below is how your source code should look like.
<head>
<link href="http://ksylvest.github.io/jquery-growl/stylesheets/jquery.growl.css" rel="stylesheet" type="text/css">
</head>
<body>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src='http://ksylvest.github.io/jquery-growl/javascripts/jquery.growl.js' type='text/javascript'></script>
<script type="text/javascript">
$(function() {
$.growl({
title: "Growl",
message: "errits already released in SU or Submitted for SU!"
});
});
</script>
</body>
Also, read the Installation step in jQuery growl website here
update
You have two jQuery libraries included which is causing the issue.
The cause of the issue is due to the multiple reference of the jquery files. The jquery file after the growl will recreate the $ instance and hence the growl that already registered was removed.
Please remove that or use jquery conflict.

How to reflect JStree changes to the original XML?

I'm using JStree to display an XML file, and after displaying it the user can create, rename, or delete some nodes, and sor sure these changes appear only on the displayed xml and not reflected on the original file, I want to know how to reflect this changes to the original XML fie back after clicking like a submit button, or at least how to get the data changed.
Update
The code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>menu-editor</title>
<script type="text/javascript" src="src/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="src/jstree/jquery.cookie.js"></script>
<script type="text/javascript" src="src/jstree/jquery.hotkeys.js"></script>
<script type="text/javascript" src="src/jstree/jquery.jstree.js"></script>
<link href="themes/!style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="src/jstree/!script.js"></script>
<script type="text/javascript" src="src/UIMTreeProcessor.js"></script>
<style type="text/css">
.
.
.
//css stuff
</style>
</head>
<body>
<div id="dummy"></div>
<div id="mylist">
<li id='root'><a href='#'>Root node</a><ul><li><a href='#'>Child node</a></li></ul></li>
</div>
<div id="submit" class="submit_btn">Submit</div>
</body>
<script type="text/javascript" class="source below">
var _url = "cafe.xml";
var _uimTree = null;
$(function () {
getTopContent();
});
getTopContent = function(){
$.ajax({
type: "GET",
url: _url,
dataType:"xml",
cache: false,
beforeSend:function(){
//do something before send
},
success: function(data){
processXML(data);
},
error:function(e){
alert("Error: "+e);
}
});
}
processXML = function(root){
_uimTree = new UIMTreeProcessor(root, $("#mylist"));
_uimTree.doProcess();
}
$('#submit').on('click',function(){
//alert the entire XML after edits via (getXML)
});
</script>
And the UIMtreeprocessor Library code
function UIMTreeProcessor(data, treeEl) {
this.data = data;
this.treeEl = treeEl;
}
UIMTreeProcessor.prototype.initTree = function(data){
this.treeEl.jstree({
"json_data" : {
"data":data,
"progressive_render":"true"
},
"plugins" : ["themes","json_data","ui","crrm","cookies","dnd","search","types","hotkeys","contextmenu"],
"core":{"animation":0}
});
}
UIMTreeProcessor.prototype.doProcess = function(){
//Find root:
var _root = $(this.data).children(':first-child');
var _a_feed = new Array();
this.vsTraverse($(_root), _a_feed);
var _treedata = [{"data":_root[0].nodeName,"children":_a_feed, "state":"open"}];
this.initTree(_treedata);
}
UIMTreeProcessor.prototype.vsTraverse = function(node, arr){
var _ch = $(node).children();
for(var i=0; i<_ch.length; i++){
var _vsArr = new Array();
this.vsTraverse(_ch[i], _vsArr);
var _a_att = this.vsTraverseAtt(_ch[i]);
if(null!=_a_att){
_vsArr.push([{"data":"Attributes "+"["+_ch[i].nodeName+"]","children":_a_att, attr : { "class" : "uim_attr"}}]);
}
if(null!=_ch[i].firstChild && 3==_ch[i].firstChild.nodeType){
arr.push([{"data":_ch[i].nodeName + ": " + _ch[i].firstChild.textContent,"children":_vsArr, "state":"open"}]);
}else{
arr.push([{"data":_ch[i].nodeName,"children":_vsArr, "state":"open"}]);
}
}
}
UIMTreeProcessor.prototype.vsTraverseAtt = function(node){
var _a_atts = null;
if(null!=node.attributes && node.attributes.length > 0){
_a_atts = new Array();
for(var i=0; i<node.attributes.length; i++){
_a_atts.push(node.attributes[i].nodeName + ":" + node.attributes[i].nodeValue);
}
}
return _a_atts;
}
Edit: I was really wrong in my original answer.
There is a get_xml method in the xml_data plugin, which is supposed to do what you describe. I have not tested it myself, but if it's anything like json_data.getJSON, the output will contain all kinds of JStree metadata which you will not want to put back into your XML.

Javascript not loaded correcly order?

I have a webpage where I load some javascript files in header. Some subpage will load aditional javascript files. On the main page everything is working just fine but on the subpage I get alot of exceptions like this :
Uncaught TypeError: Property '$' of object [object Object] is not a
function
I can see that this exception occurs in details.js, voteHandler.js and 4 times in the HTML page itself. The exception is always thrown on this line :
$("document").ready(function () {
This is how the main page that works looks like :
<head>
<script type="text/javascript" src=/Scripts/jquery-1.7.1.min.js></script>
<script type="text/javascript">
//URL for voting
var _postVoteUrl = 'http://localhost:5215/Post/Vote'
//URL for tags
var _tagsUrl = 'http://localhost:5215/Post/Tags'
//Keep track of if a cascading is loading, if so, cancel submits
var cascadingControlLoading = false;
window.latestClick = '';
function IsNotDblClick(objectID) {
if (window.latestClick != objectID &&
!cascadingControlLoading) {
window.latestClick = objectID;
return true;
} else {
return false;
}
}
$(document).ready(function () {
if($('#rightCon').text().trim().length < 1)
{$('#rightCon').hide();}
});
</script>
<script type="text/javascript" src=/Scripts/jquery-ui-1.8.20.min.js>"></script>
<script type="text/javascript" src=/Scripts/jquery.elastic.source.js></script>
<script type="text/javascript" src=/Scripts/jquery.validate.min.js></script>
<script type="text/javascript" src=/Scripts/jquery.validate.unobtrusive.min.js></script>
<script type="text/javascript" src=/Scripts/jquery.qtip.min.js></script>
<script type="text/javascript" src=/Scripts/formhandler.js></script>
<script type="text/javascript" src=/Scripts/taghandler.js></script>
<script src="/Scripts/voteHandler.js"></script>
<script type="text/javascript" src=/Scripts/select2.min.js %>"></script>
<script>
function TogglePostCon() {
$('#postListEditorCon').toggle();
}
SetupTagTextBox("txtTagBox", false);
SetupTagTextBoxPersonalTag("txtPersonalTagBox", true);
SetupTagTextBoxPersonalTag("txtPersonalIgnoreTagBox", true);
</script>
<script src="/Scripts/modernizr-2.5.3.js"></script>
</head>
And this is the subpage that throws the exceptions :
<head>
<script type="text/javascript" src=/Scripts/jquery-1.7.1.min.js></script>
<script type="text/javascript">
//URL for voting
var _postVoteUrl = 'http://localhost:5215/Post/Vote'
//URL for tags
var _tagsUrl = 'http://localhost:5215/Post/Tags'
//Keep track of if a cascading is loading, if so, cancel submits
var cascadingControlLoading = false;
window.latestClick = '';
function IsNotDblClick(objectID) {
if (window.latestClick != objectID &&
!cascadingControlLoading) {
window.latestClick = objectID;
return true;
} else {
return false;
}
}
$(document).ready(function () {
if($('#rightCon').text().trim().length < 1)
{$('#rightCon').hide();}
});
</script>
<script type="text/javascript" src=/Scripts/jquery-ui-1.8.20.min.js>"></script>
<script type="text/javascript" src=/Scripts/jquery.elastic.source.js></script>
<script type="text/javascript" src=/Scripts/jquery.validate.min.js></script>
<script type="text/javascript" src=/Scripts/jquery.validate.unobtrusive.min.js></script>
<script type="text/javascript" src=/Scripts/jquery.qtip.min.js></script>
<script type="text/javascript" src=/Scripts/formhandler.js></script>
<script type="text/javascript" src=/Scripts/taghandler.js></script>
<script src="/Scripts/details.js"></script>
<script src="/Scripts/voteHandler.js"></script>
<script>
$(function () {
//Google +1
$.getScript("http://apis.google.com/js/plusone.js", null, true);
//Twitter
$.getScript("http://platform.twitter.com/widgets.js", null, true);
//Facebook
$.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1", function () {
$('body').append('<div id="fb-root"></div>');
FB.init({ status: true, cookie: true, xfbml: true });
}, true);
});
</script>
<script src="/Scripts/modernizr-2.5.3.js"></script>
</head>
I hade some of the scripts loaded at the bottom of the body before and this did not generate the exception but from what I read this is not a recomended way to go.
So why is my subpage generating these exceptions?
In "no-confict" mode, the $ shortcut is not available and the longer jQuery is used, i.e.
jQuery(document).ready(function ($) {
By including the $ in parenthesis after the function call you can then use this shortcut within the code block.
Replace your code
$(document).ready(function () {
if($('#rightCon').text().trim().length < 1)
{$('#rightCon').hide();}
});
with this
jQuery(document).ready(function ($) {
if($('#rightCon').text().trim().length < 1)
{$('#rightCon').hide();}
});

Javascript Geo Redirection not working

My code gives a blank page. no redirection occurs
<script src="http://code.jquery.com/jquery-latest.js"></script>
<SCRIPT LANGUAGE='JavaScript'>
$.getJSON( "http://smart-ip.net/geoip-json?callback=?", function(data){
if (data.countryCode == "CA") {
window.location = 'canada.php';
} else {
window.location = 'usa.php';
}
</SCRIPT>
You script is missing some termination code.
Place this }); right before the </SCRIPT>.
So your code should look like this...
<script src="http://code.jquery.com/jquery-latest.js"></script>
<SCRIPT LANGUAGE='JavaScript'>
$.getJSON( "http://smart-ip.net/geoip-json?callback=?", function(data){
if (data.countryCode == "CA") {
window.location = 'canada.php';
} else {
window.location = 'usa.php';
}
});
</SCRIPT>

Facebox doesn't work when content is added via jQuery?

I have a website that loads content via jQuery. When loading all my javascript and css files needed for facebox. It I put a link on the initial page, it works fine. However, when changing the 'innerHTML' of one element, the same links do not function.
And I even tried putting the javascript & css files in the innerHTML and it still doesn't work! It loads up as a new page.
Please help!
Thanks
Edit 1:
index.php
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="http://justtwobros.com/actions.js"></script>
<link href="src/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="lib/jquery.js" type="text/javascript"></script>
<script src="src/facebox.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox({
loadingImage : 'src/loading.gif',
closeImage : 'src/closelabel.png'
})
})
</script>
text (this link is the one that works)
<script>
showAllWorkOnLoad();
</script>
actions.js
function showAllWorkOnLoad() {
var thePostData = "filter=allwork";
$.ajax({
type: "POST",
url: "http://justtwobros.com/get_work.php",
data: thePostData,
success: function(theRetrievedData) {
document.getElementById('content').innerHTML = theRetrievedData;
$("#content").fadeIn(20);
focusButtonAllWork();
var count = 0;
$("#content").find('.work_thumbnail_wrapper').each(function() {
count++;
var timeWait = (count * 100) + 500;
var theId = $(this).attr('id');
var theIdDone = "#" + theId;
setTimeout(function(){$(theIdDone).fadeIn(300);},timeWait);
});
}
});
}
get_work.php
text (this link that doesn't work)
It doesn't work because that link does not have a listener attached to it. You can confirm this is your problem by adding this code after your ajax call (on success, after you have inserted the link into the document).
$('a[rel*=facebox]').facebox({
loadingImage : 'src/loading.gif',
closeImage : 'src/closelabel.png'
})

Categories

Resources