# Dashcode Question...



## qbkd10 (Dec 17, 2007)

Would it be possible, in Dashcode for Leopard, to create a glass button on a widget to be linked to a URL?

Thanks in advance.

~Qbkd10


----------



## ElDiabloConCaca (Dec 22, 2007)

Yes, just add an "onClick" event to the button, then have the onClick call the following function:

```
function openURL()
{
  if ( window.widget )
  {
    widget.openURL( &#8220;http://www.google.com&#8221; );
    return false;
  }
}
```
...replacing "http://www.google.com" with whatever web address you want.


----------



## qbkd10 (Dec 22, 2007)

Parse error. It's got something to do with     widget.openURL( &#8220;http://www.google.com&#8221; );


----------



## qbkd10 (Dec 22, 2007)

Wait... wait... wait. Did some more ADC research and found that this works:


```
function openURL(section)
    {
        if (widget)
        {
            widget.openURL('http://www.quinnyblog.com' + section);
        }
    }
```

...when put into the [WidgetName].js file at the bottom. To avoid confusion, you can replace openURL with whatever you want, even elDiablo! Just make sure to link your button back up.

*EDIT:* Now, the button is adding some stuff to the end of the URL, anyone that can help me with that?
*DOUBLE EDIT:* Woo woo! Sorry, but I may sound like a complete idiot. I got it to work! This line of code:

```
{
            widget.openURL('http://www.quinnyblog.com' + section);
        }
```
... can be replaced with:

```
{
            widget.openURL('http://www.quinnyblog.com');
        }
```
It's easier than i originally thought! That little strip of code can be so pesky. What does it mean by + section, anyway?


----------



## ElDiabloConCaca (Dec 22, 2007)

"+ section" just adds the value of the variable "section" to the end of the URL.

So, for example, if you had this somewhere prior to the function:

```
var section = "/somesite.html"
```
Then, the variable "section" would evaluate to the text "/somesite.html".  Then, when you call the openURL function with:

```
widget.openURL('http://www.quinnyblog.com' + section)
```
...you would actually be opening the URL "http://www.quinnyblog.com/somesite.html"

The + operator simply concatenates the two strings together.  You have one string, explicitly defined as "http://www.quinnyblog.com" and another string, defined in the variable "section."  When you "plus" or "add" them together as in the code examples, you're simply concatenating the two together to make one, long string... the same as simply calling:

```
openURL('http://www.quinnyblog.com/somesite.html')
```
You could also store the WHOLE string as a variable, or as multiple variables.  You could do this:

```
var first = 'http://'
var second = 'www.yo-i-am-the-coolest-ever.com'
var third = '/you-better-believe-it.html'

openURL(first + second + third)
```
...and the openURL function would basically be this:

```
openURL('http://www.yo-i-am-the-coolest-ever.com/you-better-believe-it.html')
```

Make sense?


----------



## qbkd10 (Dec 23, 2007)

The first part I didn't, but the bottom half about placing the variables did! Thanks a lot. I also think you should use variables when you have multiple buttons linking to the same site., if you know what I mean.

I.E., one links to http://www.quinnyblog.com/index.html, while another links to http://www.quinnyblog.com/games.html.


----------



## ElDiabloConCaca (Dec 23, 2007)

Yep, so the variable "section" is getting set to either "/index.html" or "/games.html" somewhere in the code prior to the openURL function getting called.

I'm not sure I understand "you should use variables when you have multiple buttons linking to the same site" -- are you sure you don't mean the opposite, which would be using ONE button to link to multiple sites?  You could use a variable called "link" and do this:

```
widget.openURL(link)
```
...and then, before you call that, you could set the variable "link" to wherever you wanted to go.  Say a person using your widget wanted to visit one of two sites: either apple.com or microsoft.com.  Depending on which they wanted, you could either set the variable "link" to "http://www.apple.com" or "http://www.microsoft.com", then just call "openURL(link)" and it would go to whatever site the variable "link" was storing.


----------

