Javascript button and document.location

12,680

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.

Share:
12,680
user1453638
Author by

user1453638

Updated on June 04, 2022

Comments

  • user1453638
    user1453638 almost 2 years

    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
    user1453638 almost 12 years
    Thanks a lot! this is the unique answer that had help me! I didn't know that the variables in javascript are not global.
  • Fabrício Matté
    Fabrício Matté almost 12 years
    @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.
  • T.J. Crowder
    T.J. Crowder almost 12 years
    @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. :-)
  • Fabrício Matté
    Fabrício Matté almost 12 years
    Oh, I've found a related question which was properly answered too: stackoverflow.com/a/275102/1331430 :)
  • T.J. Crowder
    T.J. Crowder almost 12 years
    @FabrícioMatté: Looks like both work with quite broad support.
  • Fabrício Matté
    Fabrício Matté almost 12 years
    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). =]
  • T.J. Crowder
    T.J. Crowder almost 12 years
    @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.
  • Fabrício Matté
    Fabrício Matté almost 12 years
    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