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 ...
LVL 25
dgrafxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
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
0
dgrafxAuthor Commented:
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?
0
RobOwner (Aidellio)Commented:
Have you tried console.dir(your object)?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

RobOwner (Aidellio)Commented:
If you could site the $.each code you used, that would help.
Was it just

$.each(qstr, function(i,el) {
    console.dir(el);
});
0
dgrafxAuthor Commented:
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 ...
0
_agx_Commented:
> 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

0
dgrafxAuthor Commented:
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
0
_agx_Commented:
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.
0
dgrafxAuthor Commented:
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
0
_agx_Commented:
On the calling page, create a structure and serialize it as JSON. Be sure to url encode it before passing it to the target page:
      

    <cfset data = {}>
    <cfset data["abc"] = 123>
    <cfset data["efg"] = 456>
    <cfset url.qStr = serializeJSON(data)>
     
    Raw query string: <cfoutput>#url.qStr#</cfoutput>

Open in new window


On the target page, use jQuery.parseJSON to deserialize the string into an object.
 
   
    <html>
    <head> 
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
            <cfoutput>
            #toScript(url.qStr, 'qStr')#;
            </cfoutput>
            $( document ).ready(function() {
                    try {
                            $.each( jQuery.parseJSON(qStr), function( index, value ) {
                                    alert( index + ": " + value );
                            });
                    }
                    catch(e) {
                            alert(e.message);
                    }
            });    
    </script>
    </head>
    <body>
    </body>
    </html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_agx_Commented:
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.
0
dgrafxAuthor Commented:
great and thanks!
btw it does work with var qstr = '#qstr#' as well as toString ...

the urlencodedformat was the clincher i believe!
thanks again!
0
_agx_Commented:
Cool, glad it worked out.
0
RobOwner (Aidellio)Commented:
Thanks agx
0
dgrafxAuthor Commented:
thank you too Rob!
0
_agx_Commented:
Welcome @Rob :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.