Forum Moderators: open

Message Too Old, No Replies

Add open new window event to existing script

         

Meancode

9:48 am on Aug 10, 2009 (gmt 0)

10+ Year Member



I have a script I am using to create short URLs and send them to the status field in Twitter. Only I would like for this to happen in a new browser window, preferably sized to a specific height and width. I do not know how to do that. Can that functionality be added to the existing bit.ly script? I am sure it is more complicated than just using window.open.

<script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=LOGIN&apiKey=APIKEY"></script>
<script type="text/javascript">
BitlyCB.shortenResponse = function(data) {
var s = '';
var first_result;
// Results are keyed by longUrl, so we need to grab the first one.
for (var r in data.results) {
first_result = data.results[r]; break;
}

// Now get the shortURL
shortURL = first_result["shortUrl"];
document.location = "http://www.twitter.com/home/?status=" + document.title.replace(/ /g,"+") + "+" + shortURL + "+(via+@example)";
}
</script>
<img style="cursor: pointer;" src="/images/tweet.gif" title="Tweet about <$MTEntryTitle encode_html="0">" onClick="BitlyClient.shorten(document.location, 'BitlyCB.shortenResponse');">

whoisgregg

6:51 pm on Aug 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where you currently change the document.location to the new URL, just replace that line with a call to window.open. A search in your favorite search engine for "javascript window.open" will bring up plenty of examples for how to structure that code. :)

Meancode

7:58 am on Aug 12, 2009 (gmt 0)

10+ Year Member



Well, I have tried about five different ways and I cannot get it to work. This is one of the examples I found:

window.open ("http://www.example.com","mywindow");

Now if I put that on an onClick event, it works. I just do not know how to make it work with the above code, after changing document.location in both places in the script.

So, can you help me out here?

whoisgregg

1:58 pm on Aug 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take your current line of code that changes the current document.location:

document.location = "http://www.twitter.com/home/?status=" + document.title.replace(/ /g,"+") + "+" + shortURL + "+(via+@example)"; 

And change it up to call window.open instead:

windowOpenUrl = "http://www.twitter.com/home/?status=" + document.title.replace(/ /g,"+") + "+" + shortURL + "+(via+@example)"; 
window.open(windowOpenUrl, 'new_window', 'width=600,height=400');

The other place you have document.location you want to leave how it is. That part sends the current URL to your URL shortening service so it can generate a new short URL. :)

Meancode

6:51 pm on Aug 12, 2009 (gmt 0)

10+ Year Member



Yea, that is above my head. Thanks for the help, I will give it a shot.

Meancode

7:26 pm on Aug 12, 2009 (gmt 0)

10+ Year Member



Hi Gregg,

It works great in Firefox. It does not do anything in Safari.

:(

whoisgregg

9:12 pm on Aug 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have "Block Pop-Up Windows" enabled in the Safari menu? If so, this particular approach will be blocked. Basically, you can have the onclick call a function that opens a new window but you can't have the onclick call a function whic calls a second function that opens a new window.

I'm not sure the best way to get around this... legitimate uses for window.open sometimes require the user to temporarily turn off their pop-up blocker.

Meancode

6:31 am on Aug 13, 2009 (gmt 0)

10+ Year Member



I understand. And I did turn off block popups in Safari and it works now in Safari.

Thanks so much for your help with the script! Now I just have to figure out if I really want it to pop in a new window and possibly not work for some Safari users.

whoisgregg

3:20 pm on Aug 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Happy to help. :)