473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

How does this method work?

The last time I tried to ask this question...Google Groups screwed up
my message and there was no subject (sorry for that - I know it's
annoying).

I'm trying to learn how to develop a plug-in that allows users to
display data from one site in a third party site, like Google Adwords
or the Digg counter for news stories. I took a look at Digg.com and
found the following:

It looks like digg is doing the following to show the number of "diggs"
for a story on your website. On my page I would have the following:
-----------------------------------------------
<script>
digg_url = 'http://www.mynewspage.com/mystory.php';
</script>
<script src="http://digg.com/api/diggthis.js"></script>
------------------------------------------------

diggthis.js looks like:
------------------------------------------------
(function(){
var u=typeof digg_url=='string'?digg_url:DIGG_URL;
document.write("<iframe
src='http://www.digg.com/api/diggthis.php?u="+
escape(u)+"' height='80' width='52' frameborder='0'
scrolling='no'></iframe>");
})()
------------------------------------------------

A couple questions:
1. Why the extra paranthesis? Is that an attempt to throw people off?
2. How does this work: var u=typeof
digg_url=='string'?digg_url:DIGG_URL;

Thanks for your insight!

Jan 19 '07 #1
2 1516
Greg Scharlemann wrote:
The last time I tried to ask this question...Google Groups screwed up
my message and there was no subject (sorry for that - I know it's
annoying).

I'm trying to learn how to develop a plug-in that allows users to
display data from one site in a third party site, like Google Adwords
or the Digg counter for news stories. I took a look at Digg.com and
found the following:

It looks like digg is doing the following to show the number of "diggs"
for a story on your website. On my page I would have the following:
-----------------------------------------------
<script>
digg_url = 'http://www.mynewspage.com/mystory.php';
</script>
<script src="http://digg.com/api/diggthis.js"></script>
------------------------------------------------

diggthis.js looks like:
------------------------------------------------
(function(){
var u=typeof digg_url=='string'?digg_url:DIGG_URL;
document.write("<iframe
src='http://www.digg.com/api/diggthis.php?u="+
escape(u)+"' height='80' width='52' frameborder='0'
scrolling='no'></iframe>");
})()
------------------------------------------------

A couple questions:
1. Why the extra paranthesis? Is that an attempt to throw people off?
No. It is a function statement that is executed immediately. The outer
brackets causes the function statement to be evaluated and returns an
anonymous function object. The final () causes that object to be
executed.

Since the whole thing is anonymous and it doesn't create any closures
or references from itself to other objects, once it's finished
executing it will be eligible for garbage collection. It's just a tidy
way of doing something that has no chance of a name conflict and
doesn't leave a useless object floating around. It's more or less
equivalent to doing something like:

function foo(){...}
foo();
foo = null;

Except that here there's a chance (however slim) that foo will
overwrite some previously declared foo.

2. How does this work: var u=typeof
digg_url=='string'?digg_url:DIGG_URL;
That is an example of using a ternary operator to assign a value to a
variable. It takes three arguments: if the first argument is true, the
second argument is returned; otherwise the third is returned. It's
easier to read as:

var u = (typeof digg_url == 'string')? digg_url : DIGG_URL;

Which means if digg_url is a string, assign its value to u. Otherwise,
assign the value of DIGG_URL (which presumably is set elsewhere).
--
Rob

Jan 19 '07 #2

RobG wrote:
Greg Scharlemann wrote:
The last time I tried to ask this question...Google Groups screwed up
my message and there was no subject (sorry for that - I know it's
annoying).

I'm trying to learn how to develop a plug-in that allows users to
display data from one site in a third party site, like Google Adwords
or the Digg counter for news stories. I took a look at Digg.com and
found the following:

It looks like digg is doing the following to show the number of "diggs"
for a story on your website. On my page I would have the following:
-----------------------------------------------
<script>
digg_url = 'http://www.mynewspage.com/mystory.php';
</script>
<script src="http://digg.com/api/diggthis.js"></script>
------------------------------------------------

diggthis.js looks like:
------------------------------------------------
(function(){
var u=typeof digg_url=='string'?digg_url:DIGG_URL;
document.write("<iframe
src='http://www.digg.com/api/diggthis.php?u="+
escape(u)+"' height='80' width='52' frameborder='0'
scrolling='no'></iframe>");
})()
------------------------------------------------

A couple questions:
1. Why the extra paranthesis? Is that an attempt to throw people off?

No. It is a function statement that is executed immediately. The outer
brackets causes the function statement to be evaluated and returns an
anonymous function object. The final () causes that object to be
executed.

Since the whole thing is anonymous and it doesn't create any closures
or references from itself to other objects, once it's finished
executing it will be eligible for garbage collection. It's just a tidy
way of doing something that has no chance of a name conflict and
doesn't leave a useless object floating around. It's more or less
equivalent to doing something like:

function foo(){...}
foo();
foo = null;

Except that here there's a chance (however slim) that foo will
overwrite some previously declared foo.

2. How does this work: var u=typeof
digg_url=='string'?digg_url:DIGG_URL;

That is an example of using a ternary operator to assign a value to a
variable. It takes three arguments: if the first argument is true, the
second argument is returned; otherwise the third is returned. It's
easier to read as:

var u = (typeof digg_url == 'string')? digg_url : DIGG_URL;

Which means if digg_url is a string, assign its value to u. Otherwise,
assign the value of DIGG_URL (which presumably is set elsewhere).
--
Rob
Rob

Thanks for the great explanations. With your help, I've been able to
setup something similar to what digg has done. Thanks again!

Jan 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rick | last post by:
The following basic script works fine in firefox by not in IE. Can anyone spot the problem? In IE I can only delete the first line but not the lines created by javascript. Also, look at the HTML...
11
by: Michi Henning | last post by:
Hi, I'm using a blocking Select() call on a socket with a timeout value of -1. I'd expect the call to block indefinitely, but it doesn't. When I use Poll() instead, a timeout of -1 works fine...
3
by: Harry | last post by:
Hi, Can anyone tell me why the below code does not work. I dont get any errors, but nor does the Text for the asp:label get set. Thanks in advance as always H
10
by: April | last post by:
Hope someone can help me The SQL string i need to use does not work, Who can help me.... Thanks in advance Dim strConn As String Dim strSQL As String
4
by: Jon | last post by:
Hi, I used XslCompiledTransform with the following Xsl file. The <xsl:text disable-output-escaping="yes"does not work when using XslCompiledTransform to do the trnasform (namely the output...
5
by: antonyliu2002 | last post by:
Hi, It looks like so many people are having problems with the javascript submit in firefox. I searched around, but haven't found a solution yet. Mostly, people were saying, try this or try...
2
by: sauve mark | last post by:
exec sp_prepare
0
by: roberto | last post by:
Sorry, but posting a reply does no work (6 hours after the "successful" post the message is not shown)) so I decided to post it. This is a reply to a thread...
12
by: Gagan | last post by:
Hi All, I am new to VB.NET, and am confused by following code. The code basically obtains an instance of an object from a helper method. This helper method instantiates a new object, and returns...
0
by: ramveers | last post by:
I ma creating a toolbar for IE. I am setting a parent of toolbar dynamically by using SetParent method of window API. My problem is that SetParent method does not set parent and return zero(IntPtr)....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.