473,951 Members | 2,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does this method work?

The last time I tried to ask this question...Goog le 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=='stri ng'?digg_url:DI GG_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=='stri ng'?digg_url:DI GG_URL;

Thanks for your insight!

Jan 19 '07 #1
2 1546
Greg Scharlemann wrote:
The last time I tried to ask this question...Goog le 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=='stri ng'?digg_url:DI GG_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=='stri ng'?digg_url:DI GG_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...Goog le 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=='stri ng'?digg_url:DI GG_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=='stri ng'?digg_url:DI GG_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
11966
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 code for the first line (click the Table HTML button:)) you will fine that the code displayed is not the same as was written. "onChange" was changed to "onchange" etc. Please help. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">...
11
1892
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 and blocks indefinitely. The net effect is that I cannot write a select on more than one file descripter if I want to block. (With timeout values >= 0, both Select() and Poll() work fine.)
3
2656
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
1923
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
9193
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 contain < not <), while it works when using MSXML2 to do the transform. Does anyone have the same problem and how to make the escape work? Thanks. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
5
77690
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 that or maybe blah blah or why do you wanna do that blah blah. I haven't seen a solution to this problem. OK, I am trying to share session objects between classic asp applications and asp.net applications. If you insist in asking why I
2
2551
by: sauve mark | last post by:
exec sp_prepare
0
1204
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 http://groups.google.pl/group/microsoft.public.dotnet.framework.webservices/browse_thread/thread/1de13b5ef20c837a/78006eaa8e840fd1#78006eaa8e840fd1 Web Service API is a minimal one, there is nothing to refactor. What I want is to hide complexities of the proxy from third-party...
12
1367
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 it. But in its Finally block, it sets the returned object to Nothing. What perplexes me is that caller receives a valid object instance. The queston is: does this work because I am lucky, or this is really an expected behavior? Also, is this a...
0
1557
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). By using last error code, its gives 1400 (Means Invalid Handle). I check that handle of toolbar and handle of new parent are valid, but previous parent of toolbar handle has been invalid. Now i want to assign new parent of toolbar but SetParent...
0
10173
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10703
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9906
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6237
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6357
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4555
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.