Link to home
Start Free TrialLog in
Avatar of dgrafx
dgrafxFlag for United States of America

asked on

syntax and how to help

Part 1
We have a js function that loads a colorbox sending an object (qstr) with the url to the href setting
heres a simple ex:
qstr={abc:105487,xyz:18199};
href: '/testing.cfm?qstr=' + qstr

Part 2
now on testing.cfm from the above if i dump url.qstr it says it's [object Object] but cant figure out how to parse through it.
when trying to use $.each i get the "cannot use 'in' operator to search for ..." error

insight is needed ...
Avatar of Rob
Rob
Flag of Australia image

now on testing.cfm from the above if i dump url.qstr it says it's [object Object] but cant figure out how to parse through it.
when trying to use $.each i get the "cannot use 'in' operator to search for ..." error
Can you please show your code for this?  testing.cfm is run on the server, while the $.each is run on the client
Avatar of dgrafx

ASKER

hi Rob - there really isn't any CF code which is why I didn't include the CF zone
the only CF is where I turn url.qstr back into a js object

qstr = '#url.qstr#';

insight?
Have you tried console.dir(your object)?
If you could site the $.each code you used, that would help.
Was it just

$.each(qstr, function(i,el) {
    console.dir(el);
});
Avatar of dgrafx

ASKER

yes i used similar
$.each(qstr, function(i,el) {
    console.log(el);
});

i also used console.log(qstr) which equals [object Object] but no option to expand to see it's contents so am wondering if it really is an object?

i don't need to do this exactly like I've shown so if you have a better method of sending the original object - which will be something like {abc:105487,xyz:18199} - through the url and parsing through it on the landing page.
I'm leaving right now for work and can post more when I get there so forgive the delay if you post back ...
> qstr = '#url.qstr#';

@dgrafx - Quick thought.  I don't think it is an object (from javascript's POV). Due to the surrounding quotes, it looks like it's assigning a simple string. That's why an error occurs when trying to loop through it.

ie  
 // assigns string
       var qstr = '{abc:105487,xyz:18199}';

       // assigns object
       var qstr = {abc:105487,xyz:18199};

Open in new window

Avatar of dgrafx

ASKER

right!

this is the thing
for testing if i code exactly this on the landing page:
var qstr = {abc:105487,xyz:18199};
$.each(qstr, function(i,el) {
    console.log(el);
});
then no problem - good to go

But what I need to do is send this "object" in the url to the landing page
Thus the issue as to what to do with #url.qstr#
i.e. how to loop through it just as if it was created like var qstr = {abc:105487,xyz:18199};

thanks
Can you change the format of URL.qstr, so it's valid JSON? Then run it through JSON.parse, and voila its' an object.

Other than that, the only thing I'm coming up with is to eval() the string, but ... that's not a great idea at all.
Avatar of dgrafx

ASKER

can you post an example?
and yes I can change the format

one of the first things i did was to try and send in valid json as in: {"abc":"10587","xyz":"18199"}
but still seems to convert to a string on landing page when translated i.e. #url.qstr#

thanks
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think it'll work using a plain output:

      var qStr = '#url.qStr#';


... instead of this:

  <cfoutput>
      #toScript(url.qStr, 'qStr')#;
  </cfoutput>

but didn't actually test the former method.
Avatar of dgrafx

ASKER

great and thanks!
btw it does work with var qstr = '#qstr#' as well as toString ...

the urlencodedformat was the clincher i believe!
thanks again!
Cool, glad it worked out.
Thanks agx
Avatar of dgrafx

ASKER

thank you too Rob!
Welcome @Rob :)