Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


1818 posts

Uber Geek
+1 received by user: 52

Trusted

Topic # 59805 12-Apr-2010 23:55
Send private message

I am a bad coder and am writing some shocking code that I build by googling until it worked.

My current bit of php is a twitter tool that uses jquery to load() in tweet threads on demand.

I've got the base code from the jquery .load() page

And looks like this


$("#clicks").click(function(){
$("#success").load("getTweet.php",{id:7245855505}, function(response, status, xhr ) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
alert(msg + xhr.status + " " + xhr.statusText);
}
});
});

So my problem is, the above will have to be duplicated for each tweet that is a reply,
is there a way around the duplication as it will make the code look messy as with lumps of JS generated through out the page. Or is it ok to have a mess behind the scene if the user up front likes its function.

Create new topic
Infrastructure Geek
4056 posts

Uber Geek
+1 received by user: 195

Trusted
Microsoft NZ
Subscriber

  Reply # 317589 13-Apr-2010 01:34
Send private message

what about a combo of regular javascript and jquery? use the 'this' operator in the onclick to pass the current element context to the js function. grab the id and innerText (or whatever) and manipulate from there

script
function getTweet(obj){
$(#obj.id).load('getTweet.php','id:' + obj.innerText + '}', blah....
}
/script

div onclick='getTweet(this)' 7245855505 /div

As far as the end use goes, they wont care as long as it works. As a developer, less is always more. As far as speed goes, loading 100 lines of text/javascript instead of 10 probably isnt going to make much diff bandwidth wise, but it may slow a little in the browser depending on the script and the jit compiler. If you have a javascript scanning antivirus engine installed then the page loads could become considerably worse.




Technical Evangelist
Microsoft NZ
about.me/nzregs
Twitter: @nzregs




1818 posts

Uber Geek
+1 received by user: 52

Trusted

  Reply # 317896 13-Apr-2010 16:21
Send private message

Thanks Regs, been learning some more about JS from trying to understand what JS is doing from your code and I'm slowly getting there. Got the $('#'+obj.id).load sorted and should work out great.


function getTweet(obj){
$('#'+ obj.id).load("getTweet.php",{id:+obj.id}, function(response, status, xhr ) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
alert(msg + xhr.status + " " + xhr.statusText);
}
});
};

Create new topic

Twitter »

Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.