Friday, April 8, 2011

Passing functions to Document.write()

Be advised, reading this post may result in lulz:

As noted in the past few posts, I'm still perfecting that oracle. I actually want to add an option to make it search the user's string and reply with a relevant (but still somewhat random) response. Anyway, I haven't gotten that far yet because of my stubbornnes. You see, I decided from the beginning that I would make the application load from one page and use document.write() to create the "answer" page.

The above worked great until I decided that I'd get ambitious and make the thing a facebook app. Setting up the JavaScript SDK wasn't hard at all, but getting that SDK to work in the document.write() html was somewhat of a challenge. No matter what I did, I couldn't get the example function provided for FB.ui to post in it. I spent hours readjusting the code placing it in all kinds of different places. I even went as far to redeclare the function fb_publish(); inside of a script within the document.write() html! Then, after that failed, I went around adding backslashes to escape everything in the function. I still don't understand why -that- failed..

As mentioned before, I was being pretty stubborn about the whole "one file" thing. The solution was a deceptively simple one: create an external .js file, include it in the document.write() html's head section. Then, you can call it from document.write(); with impunity! Cheers!

The last question before I start working on making the thing give relevant responses is passing variables to FB.ui (user name, oracle response, etc.). But my host's site is down atm (and facebook won't let that FB.ui execute unless it's on app's site), so I thought I'd take the time to rant about this.

No comments:

Post a Comment