1

I'm trying to do something with javascript (I'm a beginner and I'm studying it) and I'd like to know how can I open a link saved in a variable. I'm trying with ...

<input type="button" onclick="document.location.href=Query;" />

Where Query is a variable in the method Ricerca that works with another button

function ricerca()
        {
            var Link = "http://www.mysite.com/search?q=variabile&k=&e=1";

            var Name= document.getElementById('utente').value;

            var Query = Link.replace("variabile",Name);

            alert(Query); 
            return false;
        } 

the other button generates a custom search link ...

input type="text" id="utente">
<input type="submit" value="Click me" onclick="return ricerca();" /> 

What's wrong with my code?

user1453638
  • 141
  • 2
  • 3
  • 11

1 Answers1

5

This markup:

<input type="button" onclick="document.location.href=Query;" />

...would require that you have a global variable called Query, which you don't have. You have a local variable within a function. You'll need to have a function (possibly your ricerca function?) return the URL, and then call the function. Something like this:

function ricerca()
{
    var Link = "http://www.mysite.com/search?q=variabile&k=&e=1";

    var Name= document.getElementById('utente').value;

    var Query = Link.replace("variabile",Name);

    return Query;
} 

and

<input type="button" onclick="document.location.href=ricerca();" />

Separately, just use location.href rather than document.location.href. location is a global variable (it's a property of window, and all window properties are globals), and that's the one you use to load a new page.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks a lot! this is the unique answer that had help me! I didn't know that the variables in javascript are not global. – user1453638 Jun 18 '12 at 07:06
  • @T.J.Crowder One last question, wouldn't setting the `location` be better than `location.href`? As far as I remember, `location.href` is read-only in certain (older) browsers and I've solved many issues by changing `location.href` to `location` when handling redirects like this one. – Fabrício Matté Jun 18 '12 at 07:10
  • @FabrícioMatté: I'm afraid I don't know. I've almost never done it, and honestly can't recall whether, on the rare occasions when I have, I set `location` or `location.href`. :-) – T.J. Crowder Jun 18 '12 at 07:13
  • Oh, I've found a related question which was properly answered too: http://stackoverflow.com/a/275102/1331430 `:)` – Fabrício Matté Jun 18 '12 at 07:14
  • @FabrícioMatté: Looks like both work with quite broad support. – T.J. Crowder Jun 18 '12 at 07:21
  • Yes, `.href` is read-only in IE6 and FF3 only from the popular browsers list I'd guess (last time I ran into this problem was with FF3, I'll test on IE6). =] – Fabrício Matté Jun 18 '12 at 07:22
  • @FabrícioMatté: No, it's not even read-only on IE6 (I tested it), and I tested it on FF 3.6 as well. scunliffe said there was a "cornercase" on IE6 where it failed, but in the main case, it works. – T.J. Crowder Jun 18 '12 at 07:25
  • Heh, good to know it's more well-supported than what I thought. +1 for all your testing effort. =] Guess I've hit the 1 in a million, or 1 of the 17 people who ran into this problem so far. `:P` – Fabrício Matté Jun 18 '12 at 07:26