Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

closed as off-topic by Robert Lindgren Jan 28 at 20:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to SharePoint are off-topic here, but can be asked on Stack Overflow." – Robert Lindgren
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

up vote 0 down vote accepted

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>
share|improve this answer
    
Adding the return and the type together in the button command solved my issue. Thanks –  user22127 Jan 28 at 20:16

Try

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.

share|improve this answer

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