I have a Sharepoint2010 site and am trying to execute a Javascript code to create some drop down selection menus with a GO button at the end. The GO button is supposed to run a function that does a URL redirect via the window.location command based on the choices in the dropdown. I have this saved in a TXT file then called within a CEWP.

The snippet of code that I believe is having issues is with the function or the button itself.

This is what I have at the moment in a simplified form:

function submitGo(){
window.location = "www.google.com"
<button onclick="submitGo()">Go</button>

But when I load my page and click the GO button it just reloads my sharepoint page and does not perform the redirect. I have tried replacing the function in the command with the window.location command directly and it does the same thing. I am at a complete loss as to why this is not working.


It's hard to tell why this is happening. It could be browser-specific. We could try to prevent a post-back by including return false; at the end of your onclick function:

<button onclick="submitGo(); return false;">Go</button>

Update: also, you might need to set button's type explicitly:

<button type="button" onclick="submitGo(); return false;">Go</button>
| improve this answer | |
  • 1
    Adding the return and the type together in the button command solved my issue. Thanks – user22127 Jan 28 '14 at 20:16


window.location = "http://www.google.com";

You should also learn to take advantage of the web developer tools for javascript debugging. For example, in IE, press F12. If you then click the 'Console' tab, you have a REPL console where you can interactively enter javascript in the context of the current page. For example, you can type....

alert("Hello World")

.....and it will execute that command. Extending that example, you can type....

 window.location = "www.google.com"

..... and see that it doesnt' work.

Using this interactive console to fine-tune jQuery selectors saves me TONS of time.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.