[jQuery] Re: dealing with a post (json) callback

2009-05-18 Thread dhtml



On May 13, 1:29 pm, Peter Warnock petewarn...@gmail.com wrote:
 [ ] is an array literal, like { } is an object literal.

 var obj = json[0];

 You shouldn't useevalon JSON.  If you specify 'json' as your return
 type, jQuery will safelyevalvalid JSON.


jQuery uses what ECMA 262 calls indirect eval.

window[eval](json);

|  // Get the JavaScript object, if JSON is used.
|  if ( type == json )
|data = window[eval](( + data + ));

ECMA 262 has something to say about indirect eval, too:

| If value of the eval property is used in any way other than
| a direct call (that is, other than by the explicit use of its
| name as an Identifier which is the MemberExpression in a
CallExpression), or if the
| eval property is assigned to, an EvalError exception may be thrown.

So it seems that using jQuery here would not be as safe as using a
direct call to eval (as sneaks code uses) because the jQuery approach
may legally result in EvalError.

Further improvements to the code would be to replace the jQuery.each
call and not use the query selector. This would have the effect of
making the program smaller and faster, having the side effect of
removing some dependencies on jQuery.

Garrett

 - pw

 On May 12, 9:05 pm, sneaks deroacheee...@gmail.com wrote:



  hi, in firebug my callback data(object) is like so:

  [{product_brand:Creative Recreation,product_name:Cesario
  Hi,product_slug:CRM-CESAR-PRIME-
  HI,product_active:1,product_description:mens- red}]

  i have never noticed the square brackets before ...
  but when i attempt to use each to oterate through the obnject for
  values... i get nothing:

  var obj=eval('(' + json + ')');
  console.log( obj );
  jQuery.each(obj, function(i, val) {
    if (i) {
        alert(val);
        jQuery('#'+i).append(val);
    }

  });

  this has always worked fro me before and i am not doing anything
  different this time... but i would appreciate any insight into what
  may be happeneing here


[jQuery] Re: The jQuery Object, Namespaces, and Program Modules -- Connecting the Dots Between jQuery and Javascript

2009-05-06 Thread dhtml



On May 5, 1:53 pm, Matt Kruse m...@thekrusefamily.com wrote:
 On May 5, 3:38 pm, kiusau kiu...@mac.com wrote:

  QUESTION:  If a lone pair of parentheses can be used to automatically
  call a function, then what does it mean when two pairs are juxtaposed
  in the same statement as follows:  ()(jQuery);?

 See, this is purely a javascript language question, unrelated to
 jQuery specifically.

 In js, () around something simply returns what is inside of it. So, (x)
 ===x.


That is not entirely true, as the statement:-

x(true);

should be a interpreted as call operation, the stuff inside of the
parens is an ArgumentList in that case.

() - can be:
 *  a grouping operator
 *  an Arguments expression (for use with a [[call]] or
[[construct]] operation).
 *  part of the production for a function definition, as function(){}
(and also a JS 1.8 expression closure[1], a non-standard language
extension).

 Then,

 (function() { })

Grouping Operator.


 simply returns the anonymous function object. And how do you call a
 function? You put () after it.

 (function() { })();


Calling a function. The Arguments can also be used for construct, or
new:-

var time = new (function(x){
  this.timeStamp = +new Date;
  this.end = new Date(x); // Invalid Date.
})(Infinity);

[...]

Garrett


[jQuery] Re: Invalid Argument Line 12 1.3.2 IE 7 and 8rc1

2009-04-26 Thread dhtml



On Mar 9, 3:43 am, phipps_73 phipp...@googlemail.com wrote:
 Hi,

 I have just upgraded one of our development sites to 1.3.2 and ui 1.7.
 I have also updated all of the plugins
 (forms,validate,thickbox,calculation,blockUI,autogrow) to the latest
 versions which should all be compatible with 1.3.2 and the site is
 working perfectly in Firefox 3 on OS X and XP.

 However on IE8rc1 and in IE7 compat mode I am getting an Invalid
 Argument error reported at line 12 of jquery-1.3.2.min.js. That is the
 usual really helpful IE error message and I have been unable to track
 down the source of the problem.

Do you get the same error with the normal, non-minified version?

Garrett


[jQuery] Re: Loading before dom = ready - Best Practices.

2009-04-24 Thread dhtml



On Apr 23, 3:45 pm, Adam hedgoma...@gmail.com wrote:
  That sounds like a potential accessibility issue.

 I certainly think so, hence the question :]


Why can't you just include the content from the server?

If the goal is to have the information when the page loads, send it in
the initial request. Bots will see the links, the links will degrade,
and you will have less javascript on the page.

  Such script tags should be moved to just before /
  body.

 They are, but this still produces the problem since that's still part
 of the DOM we're waiting on.


Eliminate possibility that there is initial latency with lori FF
plugin.

You didn't say if the ads are in iframes. Either way, the ads can be
moved down and repositioned with CSS. If the ads are in frames.

One possible load order:-
body
[content]
[your scripts]
[ads]
[analytic scripts]
/body

Firebug's Net tab has request info, so you can see which request is
taking the longest. For other browsers on Windows, Fiddler provides
information about requests on a page. It could very well be an image
that your page uses. Possibly the CDN is not performing well enough.

Garrett


[jQuery] Re: Loading before dom = ready - Best Practices.

2009-04-23 Thread dhtml



On Apr 23, 12:13 pm, hedgomatic hedgoma...@gmail.com wrote:
 While virtually every site in existence trumpets using the jQuery DOM-
 ready shortcut as an absolute must, I've come across situations which
 I feel frustrate the user, particularly when using jQuery to create a
 navigational element.


That sounds like a potential accessibility issue.

 I often work on sites which are going to have a lot of external
 content (ads, feeds, analytics), and if even one of them is sluggish
 to load, none of my interactive elements are responsive for that time.

You say you've got ads, feeds, and analytics. Are the ads in iframes?
How are the analytics included? Is it something like Omniture or
ForeseeResults? Such script tags should be moved to just before /
body.


 There seem to be three options:

 1] liveQuery (disadvantage: overhead)
 2] popping a loading message over the whole page (disadvantage:
 ridiculous)
 3] nesting an image inside the portion of the DOM we need, and using
 an onLoad event (disadvantage: poor semantics).

 Anyone else come across any novel ways around this seemingly under-
 discussed issue?

There are probably more and better options. Possibly by using a
combination of a few things, the performance and user experience (and
possibly SEO) could be improved.

Garrett


[jQuery] Re: question about dealing with JSON callback

2009-04-22 Thread dhtml



On Apr 17, 10:55 am, mkmanning michaell...@gmail.com wrote:
 - the dataType is JSON, not json.
 (I pointed this out earlier in this thread)

 I noticed what sneaks did, I also noticed you had pointed it out,
 which is why my comment was in direct reply to sneaks post about the
 'object side' and removing the quotes (check the quoted text in my
 post to see that it was for that post specifically) and was predicated
 on the assumption that he'd read and applied your suggestion. It was
 simply an FYI for him that that terminology ('object side') didn't
 make sense, and that the now-resolved object works perfectly fine with
 quotes on the name portion of the name-value pairs.

 I understand how jQuery handles the response, that's why I further
 indicated using the correct type in my follow up; I also understand
 that you can't eval an object. Neither of those have anything to do
 with my direct comment to sneaks.


Actually, eval takes any value for its argument. If that value is not
a string, it is returned.

For example:-
var x = {};
alert(x === eval(x));

elerts true.

 OK. I think this clarifies that |json| is not an object, but a

 string.

 No, it clarifies that an ajax responseText formatted as JSON is a
 string, but that was never in question, as I've indicated above. As
 far as we're clarifying however:  JSON isn't a string, it's text. The
 responseText from an ajax call is a string, and it may be in JSON
 format or it may not.  If you want to quibble about whether the
 resolved object should then be called JSON that's fine, but also
 totally beside the point.


It *is* a string, not formatted as. Figuring out value of that
variable was important in debugging that.

sneaks had expected that the variable json was an object (in code and
in explanation). He had used JSON for the dataType. Using json
instead would result in the jQuery method returning an object (it is
just called an object, not a json object).

Garrett

[snipped previous post]


[jQuery] Re: question about dealing with JSON callback

2009-04-17 Thread dhtml



On Apr 16, 5:14 pm, mkmanning michaell...@gmail.com wrote:
 As I said before: it's a string until it's eval'd, which happens with
 the 'json' response type within jQuery, or as I said you can eval the

Notice carefully what 'sneaks' used for his dataType:

jQuery.post(
/wp/wp-admin/admin-ajax.php, {
action: getProductInfo,
'cookie': encodeURIComponent(document.cookie),
'product_id': product_id
   },function(obj) {
   jQuery.each(obj, function(i, val) {
jQuery('#' + i).attr('value',val);
   });
   },JSON);

}

- the dataType is JSON, not json.
(I pointed this out earlier in this thread)

So, the conditional below is false and following statement does not
execute:-

| [...]
|   if ( type == json )
| data = window[eval](( + data + ));
|   return data;
| },

What happens in this case is that jQuery.ajax does not handle a case
for dataType == JSON and passes the xhr's responseText through to
the callback function.

Identifier json holds a string value.

Sneaks resolved his issue by using eval directly, as:

var obj=eval('('+json+')');

If |json| is an object, that would be interpreted as:-

eval(([object Object]));

- which would result in EvalError.

OK. I think this clarifies that |json| is not an object, but a
string.

Garrett


[jQuery] Re: Creating custom attributes in html

2009-04-17 Thread dhtml



On Apr 14, 3:34 pm, Ricardo ricardob...@gmail.com wrote:
 If you insert these attributes server-side, the page will not validate
 and might trigger quirks mode in the browser.

Which browser?


Thanks,

Garrett


[jQuery] Re: question about dealing with JSON callback

2009-04-16 Thread dhtml



On Apr 15, 7:33 pm, sneaks deroacheee...@gmail.com wrote:
 for anyone having similar problem heres how i resolved it:

         function getProductInfo(product_id) {
                 jQuery.post(
                         /wp/wp-admin/admin-ajax.php, {
                                 action: getProductInfo,
                                 'cookie': encodeURIComponent(document.cookie),
                                 'product_id': product_id
                         },function(json) {
                                 var obj=eval('('+json+')');
                                 jQuery.each(obj, function(i, val) {
                                         jQuery('#'+i).attr('value',val);
                                 });
                 },JSON);
         }


Posting up relative paths to your PHP file does not help debug the
problem.

 the key is you need string brackets around the string json for the
 eval


Javascript has no such thing String Brackets.

 good luck

Both guesswork and luck should be required to debug your problem from
this perspective.

But the problem is not that involved. Your dataType is is JSON not
json[1].

The example jquery code you provided expects your document to have an
element with ID for each property name in the JSON response. You
should have provided a complete example.

 j

Garrett
[1]http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktype


[jQuery] Re: question about dealing with JSON callback

2009-04-16 Thread dhtml



On Apr 16, 12:42 am, mkmanning michaell...@gmail.com wrote:
 Just an FYI, but there's no 'object side' of the json in your example.
 It just an object, consisting of name-value pairs. While you can leave

No, it is not an object. It is a string.

 quotes off of the names, they are strings which, according to the RFC,
 should be quoted. Doing so will not cause problems, and will save you
 from potentially running into a situation where your name conflicts
 with one of the excessive number of reserved words.

 On Apr 15, 7:05 pm, sneaks deroacheee...@gmail.com wrote:

  the way i see it, there are quotes on the object side of the json
  where there should be no quotes...

That makes about as much sense as something the OP would post.

Garrett


[jQuery] Re: how to streamline my code with Event Delegation?

2009-04-16 Thread dhtml



On Apr 16, 12:28 pm, redsun sun...@yahoo.com wrote:
 i'm sure the jQuery i'm using below is very ineffiecient. the only

I would not doubt that it is inefficient (you are using jQuery).

 syntax that changes are the numbers in the names of my IDs and my
 variables - and even they're matching. everything else is constant.
 i'm told that event delegation is the answer, but cant rewrite the
 code appropriately (i tried, but just made a mess of things -
 rookie).


Instead, use a simple function for a document event handler
callback:-

document.onclick = documentClicked;

(it is better to us an adapter that would not replace
document.onclick, but for now, that would work for an example of how
to do it.)

In any case, check the target of that callback to see if it has an id
matching a pattern link\d\d.

To pass those \d\d to myPage.asp, use a capturing group. For example:-

/link(\d\d)/

That should appear in documentClicked.

!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01//EN http://www.w3.org/
TR/html4/strict.dtd

html lang=en
head

title/title

script type=text/javascript
function documentClicked(ev) {
  var target, targetIdExp = /link(\d\d)/;
  if(typeof ev == undefined) {
ev = window.event;
  }
  target = ev.target;
  if( typeof target == undefined) {
target = ev.srcElement;
  }

  // check matching criteria.
  if(targetIdExp.test(target.id)) {
getMyPage(RegExp.$1);
  }
}

function getMyPage(id) {
  alert(myPage.asp?id= + id);
}

document.onclick = documentClicked;
/script
/head

body

a id=link12link 12/a
div
a id=link99link 99/a
/div

/body
/html

[...]

Garrett


[jQuery] Re: question about dealing with JSON callback

2009-04-15 Thread dhtml



On Apr 14, 9:34 pm, sneaks deroacheee...@gmail.com wrote:
 hi! i am trying to use jQuery.post() to send name/value to a php
 function which then retreives data from mysql and responds with JSON
 which looks like this when i append it directly to my debug output
 div:


[...]

 i have tried to iterate through the JSON using the jQuery.each():

You should provide a reduced testcase so that we can see the problem.
This should be a live example on your webhost, ideally (so we don't
have to copy all of your files to our own webserver).

When posting code, you should also format that code to wrap at = 72
chars. Use spaces, not tabs. This makes it easier for us humans to
read.

Plain enumeration is simple. For example:-

var json = eval( responseText ),
 key, value;

for( key in json ) {
  value = json[key];
}

But you are using jQuery. So we'll try and figure out what is going
on.


 Query.each(obj, function(i, val) {
 etc...

Why don't you post up the whole function?

Garrett


[jQuery] Re: question about dealing with JSON callback

2009-04-15 Thread dhtml



On Apr 15, 4:34 pm, sneaks deroacheee...@gmail.com wrote:

[posting order restored]

 On Apr 15, 1:28 pm, dhtml dhtmlkitc...@gmail.com wrote:

  On Apr 14, 9:34 pm, sneaks deroacheee...@gmail.com wrote:

   hi! i am trying to use jQuery.post() to send name/value to a php
   function which then retreives data from mysql and responds withJSON
   which looks like this when i append it directly to my debug output
   div:

  [...]

   i have tried to iterate through theJSONusing the jQuery.each():

  You should provide a reduced testcase so that we can see the problem.
  This should be a live example on your webhost, ideally (so we don't
  have to copy all of your files to our own webserver).

  When posting code, you should also format that code to wrap at = 72
  chars. Use spaces, not tabs. This makes it easier for us humans to
  read.

  Plain enumeration is simple. For example:-

  varjson= eval( responseText ),
       key, value;

  for( key injson) {
    value =json[key];

  }

  But you are using jQuery. So we'll try and figure out what is going
  on.

   Query.each(obj, function(i, val) {
   etc...

  Why don't you post up the whole function?


 heres the function (it connects to wordpress database so i cant send a
 url)


That is not the same function. That is the code that makes the HTTP
Request to the server.

[snipped code]

In order to debug the problem, please provide a simplified test case.
You can include the data that you got back from the server as just a
variable. For example:

!doctype html
html
headtitlejson in jQuery.each/title
script type=text/javascript
 var jsonResponse = {x:1};

// rest of code here.

/script
/head
body

/body
/html

Tell us which browsers you used, the results that you got, and the
results you expected.

Without that, about all anyone can do is guess.

Garrett


[jQuery] Re: Calling a function from a non-jQuery .JS file

2009-04-14 Thread dhtml



On Apr 14, 8:58 am, Klaus Hartl klaus.ha...@googlemail.com wrote:
 On 14 Apr., 15:41, MorningZ morni...@gmail.com wrote:

  So what is the error

 The error is obviously that toDate is not a jQuery method. Another

Right.

 problem is that that Dreamweaver method is using document.write which
 can cause a problem depending on where the function is being called
 and which is usually avoided in jQuery land.

Calling document.write in a jQuery ready callback won't work
consistently.


 Since toDate is not (yet) a jQuery method, let's make one:

 (function($) {

     var days = [
         'Sunday',
         'Monday',
         'Tuesday',
         'Wednesday',
         'Thursday',
         'Friday',
         'Saturday'
     ];

     var months = [
         'January',
         'February',
         'March',
         'April',
         'May',
         'June',
         'July',
         'August',
         'September',
         'October',
         'November',
         'December'
     ];

     $.fn.toDate = function() {
         var now = new Date;
         return this.html('pToday\'s Date:br /' + days[now.getDay
 ()] +
                         ', ' + now.getDate() + ' ' + months[now.getMonth()] + 
 ' ' +
                         now.getFullYear() + '/p');
     };

 })(jQuery);

 Now you can use it like:

 $(document).ready(function() {
    $('#today').toDate();

 });


Or not. The above is fine for some locales, but not all.

If you want to format a local date, what is wrong with just having:-

var todayEl = document.getElementById(today);
todayEl.innerHTML = formatDate(new Date); // [1]

?

[1]http://www.jibbering.com/faq/#formatDate

Garrett