javascript - How to place a jQuery snippet into a global file -
i have javascript file here http://www.problemio.com/js/problemio.js , trying place jquery code looks this:
$(document).ready(function() { queue = new object; queue.login = false; var $dialog = $('#loginpopup') .dialog({ autoopen: false, title: 'login dialog' }); var $problemid = $('#theproblemid', '#loginpopup'); $("#newprofile").click(function () { $("#login_div").hide(); $("#newprofileform").show(); }); // called right away after clicks on vote link $('.vote_up').click(function() { var problem_id = $(this).attr("data-problem_id"); queue.voteup = $(this).attr('problem_id'); voteup(problem_id); //return false prevent page navigation return false; }); var voteup = function(problem_id) { alert ("in vote function, problem_id: " + problem_id ); queue.voteup = problem_id; var datastring = 'problem_id=' + problem_id + '&vote=+'; if ( queue.login = false) { // call ajax try log in...or dialog box log in. requirelogin() } else { // person logged in lets have him vote $.ajax({ type: "post", url: "/problems/vote.php", datatype: "json", data: datastring, success: function(data) { alert ("vote success, data: " + data); // try update vote count on page //$('p').each(function() //{ //on each paragraph in page: // $(this).find('span').each() // { //find each span within paragraph being iterated on // } //} }, error : function(data) { alert ("vote error"); errormessage = data.responsetext; if ( errormessage == "not_logged_in" ) { //set current problem id 1 within dialog $problemid.val(problem_id); // try create popup asks user log in. $dialog.dialog('open'); alert ("after dialog open"); // prevent default action, e.g., following link return false; } else { alert ("not"); } } // end of error case } }); // closing ajax call. }; $('.vote_down').click(function() { alert("down"); problem_id = $(this).attr("data-problem_id"); var datastring = 'problem_id='+ problem_id + '&vote=-'; //return false prevent page navigation return false; }); $('#loginbutton', '#loginpopup').click(function() { alert("in login button fnction"); $.ajax({ url:'url login', success:function() { //now call cote voteup($problemid.val()); } }); }); }); </script>
there 2 reasons why trying that:
1) guessing practice (hopefully easier keep track of global variables, etc. 2) more importantly, trying call voteup(someid) function in original code problemio.js file, , getting error undefined function, figured i'd have better luck calling function if in global scope. correct in approach?
so can copy/paste code placed question problemio.js file, or have remove parts of opening/closing tags? document.ready() function? should have 1 of in global file? or should have multiple of them , won't hurt?
thanks!!
1) guessing practice (hopefully easier keep track of global variables, etc.
yes , no, have 'global' variables in 1 spot chances you're going collide 'global' variables (ie defined browser) have increased 100% :)
for example decided have variable called location
, give variable value browser decides fly off url because location
reserved word redirecting.
the solution use namespacing, described here
2) more importantly, trying call voteup(someid) function in original code problemio.js file, , getting error undefined function, figured i'd have better luck calling function if in global scope. correct in approach?
here's example using namespacing call voteup
function:
(function($) { var myapp = {}; $('.vote_up').click(function(e) { e.preventdefault(); myapp.voteup(); }); myapp.voteup = function() { console.log("vote!"); } })(jquery);
what document.ready() function? should have 1 of in global file? or should have multiple of them , won't hurt?
you can have many document.ready listeners need, not overriding document.ready listening event fire , defining happen. have them in separate javascript files.
Comments
Post a Comment