简体   繁体   中英

Javascript function using `locatin.href = …` works when called from console, not when called from `onClick` in a button

I imagine I'm missing something simple here, but I am kind of baffled.

I'm using Firefox Aurora 28.0a2

I have a button,

<button class="submit_button" id="search_google" onClick="search('google');">Google</button>

and, in a linked js file, the function

function search(siteName) {
    location.href = "http://www.google.com";
}

(Naturally, this is only some test code, and I only expect it to open the google.com.) Now, when I click on the button, it doesn't load the url as expected. However, the button is calling the function successfully, since it'll trigger an alert if I add alert("something") to the function.

However, when I simply call the function from the console, it works exactly as expected. What I find even more baffling is that, if I change the button to an anchor, it works fine when clicked.

Any help is greatly appreciated.

It works fine with google chrome. try add top. at the begining.

top.location.href = "http://www.google.com";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM