Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a tweet button without opening a new window

I'm looking to add a "tweet this" button to a site. Simple enough, right? The catch is that the site is meant to run on an embedded platform that doesn't particularly handle popup windows, so I'm trying to do everything inside the page.

I'm able to successfully create my tweet button, attach an onClick handler to it, and construct a proper twitter.com/share URL for the relevant content. All works fine when I open that URL in a new window with window.open. However, if I try to open the URL in an iframe, nothing loads inside the frame. Even loading http://twitter.com into the iframe fails in the same way. However, loading Google or any other website seems to work just fine.

Any thoughts on what I'm missing here? Thanks! --zach

Edit: Yep, they are detecting the iframe on load and blanking the page:

if (window.top !== window.self) {
    document.write = "";
    window.top.location = window.self.location; 
    setTimeout(function(){ document.body.innerHTML='';},1);
    window.self.onload=function(evt){document.body.innerHTML='';};

}

Any reasonable way to get around this, or am I stuck writing my own auth pipeline through oauth? I don't need anything from their API, just letting users tweet to their own accounts.

like image 426
Zach Lipton Avatar asked Dec 13 '10 17:12

Zach Lipton


People also ask

Why can't I press the Tweet button?

Trouble sending Tweets can often be attributed to a need to upgrade your browser or app. If you're having trouble Tweeting via the web, make sure you're using the latest version of your browser. If you can't Tweet with an official Twitter app, check to make sure you've downloaded any available updates.

Where is the Tweet button on Twitter?

Type your Tweet (up to 280 characters) into the compose box at the top of your Home timeline, or select the Tweet button in the navigation bar.


2 Answers

Twitter (like Stack Overflow) is probably using some Javascript to ensure they're not being presented in an iFrame:

if(top!=self){
     //hates you
}

I ran into something similar recently, and ended up re-doing part of my app without the iFrame element.

like image 87
Ben L. Avatar answered Sep 27 '22 20:09

Ben L.


Go and get a developper account on twitter and things are made easy for you :)

like image 35
halfpastfour.am Avatar answered Sep 27 '22 20:09

halfpastfour.am