We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Javascript - Function Isn't Defined; Even though it is?
Topic Started: Jul 23 2010, 12:00 AM (2,157 Views)
IceMetalPunk
Member Avatar
We are all IMPerfect. Be proud!
[ *  *  *  *  *  * ]
Hey, everyone. I was just testing out Twitter's API and seeing how it works, what queries are needed, etc. I came up with a basic Javascript/AJAX approach, but I can't test it to see if it works because...well, I don't know why.

Every time I try to test this, nothing happens. Firefox's error console tells me that PostData, the function I'm using to test the Twitter API, is not defined. It only says this once I try to run it; I'm positive it IS defined. Syntax checkers say the syntax is fine, and I've triple- and quadruple-checked all my function and variable names for typos. Everything seems fine, but still, Firefox thinks PostData is undefined.

Here's all the code on the test page:

Code:
 
<script type='Javascript'>
function BuildQuery(theform) {
query="";
for (e=0; e<theform.elements.length; e++) {
if (query!=="") { query+="&"; }
query+=escape(theform.elements[e].name)+"="+escape(theform.elements[e].value);
}
return query;
}


function PostData(theform,un,pw) {
query=BuildQuery(theform);
req=new XMLHttpRequest();
req.open("POST","http://www.twitter.com/statuses/update.xml",1,un,pw);

req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", query.length);
req.setRequestHeader("Connection", "close");

req.onreadystatechange=function() {
alert('Done! Status: '+req.status);
};

req.send(query);
}
</script>

Username: <input type='text' id='UserName' /><br />
Password: <input type='password' id='PassWord' /><br />
<form name="TweetTest" onsubmit="return false" method="post">
Tweet: <input type='text' name='status' /><br/>
</form>
<input type='button' onclick="PostData(document.TweetTest, document.getElementById('UserName'), document.getElementById('PassWord'))" value='Tweet!'/>


I click the button, which calls the PostData method. As you can see, PostData is most definitely defined above, so why the heck doesn't Firefox see that?

-IMP ;) :)

P.S. I know I don't need the BuildQuery function since it's only one form field, but I like to make my methods as flexible as possible from the beginning. For example, now I can just add new fields in the future and they'll automatically be sent correctly without my needing to mess around with the AJAX.
Edited by IceMetalPunk, Jul 23 2010, 12:07 AM.
Offline Profile Quote Post Goto Top
 
Garath531
Member Avatar
Look, up in the sky! It's a bird! It's a plane! It's Superman!
[ *  *  *  * ]
Try changing
Code:
 

<script type='Javascript'>
to
Code:
 

<script type='text/javascript'>
Offline Profile Quote Post Goto Top
 
HolySavior
Member Avatar
if( holy + alcohol){ happycoding()}
[ *  *  *  *  *  *  * ]
Code:
 

<input type='button' onclick="PostData(document.TweetTest, document.getElementById('UserName'), document.getElementById('PassWord'))" value='Tweet!'/>


it could be erroring because when youa re using document.getElementById. you are grabbing that element yes but your not telling it when part of that element. basically you want the value.

so i would try
Code:
 

<input type='button' onclick="PostData(document.TweetTest, document.getElementById('UserName'.value), document.getElementById('PassWord').value)" value='Tweet!'/>[/ code]
Offline Profile Quote Post Goto Top
 
IceMetalPunk
Member Avatar
We are all IMPerfect. Be proud!
[ *  *  *  *  *  * ]
:P Yeah, I just figured out both of those problems :) . I haven't done much Web design lately, so I was reading it as "language='Javascript'" instead of what I really typed, "type='Javascript'". Thanks, though.

Though now I'm having another issue with this code. I'm getting security errors for trying to connect to Twitter via AJAX. Since Twitter's API is designed to be used off-site (and therefore I'm fairly certain they allow cross-site scripting), and since their documentation says nothing about any more authorization than a username/password combo like I'm using, can anyone think of a reason I still don't have authorization to connect? I'm positive the username/password combo is correct (it's my personal Twitter account)...

-IMP ;) :)
Offline Profile Quote Post Goto Top
 
HolySavior
Member Avatar
if( holy + alcohol){ happycoding()}
[ *  *  *  *  *  *  * ]
hmmm i tried digging alittle deeper. and i dont think you can actually use the API like that. i think if you want to use JS/AJAX you need to use PHP with it.

http://www.newwebplatform.com/tips-and-tutorials/Twitter#Javascript/AJAX

i found that if that helps alittle bit

The API presently supports the following data formats: XML, JSON, and the RSS and Atom syndication formats, with some methods only accepting a subset of these formats.

also here are some twitter libraries for JS
http://dev.twitter.com/pages/libraries#javascript
Offline Profile Quote Post Goto Top
 
Paper
Member Avatar
Member
[ *  *  *  *  *  * ]
If I'm not mistaken, the cross domain security is implemented by the browser and there's no way of overcoming that without either using a server or a cross-domain scripting attack (which would most likely not work in this case).

PHP is the way to go, have a look here: http://www.geekdaily.net/2007/08/31/twitter-javascript-api-the-beginning/



EDIT: Never mind was wrong!
Edited by Paper, Mar 25 2011, 09:45 PM.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply