admin管理员组文章数量:1414628
<button id="promoCodeSubmit" onclick="window.location.href=''+document.getElementById('promoCodeValue').value;">Apply</button>
Trying to use the code above to redirect a page. console.log is printing out the correct URL, but page gets redirected incorrectly. Any ideas why?
<button id="promoCodeSubmit" onclick="window.location.href='http://www.test.'+document.getElementById('promoCodeValue').value;">Apply</button>
Trying to use the code above to redirect a page. console.log is printing out the correct URL, but page gets redirected incorrectly. Any ideas why?
Share Improve this question asked Sep 17, 2013 at 18:45 Megan TaylorMegan Taylor 972 gold badges3 silver badges10 bronze badges 4- Hi Megan. Page gets redirected incorrectly where? Can you share more of your code so we can see what console.log is picking up, and what is ending up in the redirect? – Brett Commented Sep 17, 2013 at 18:47
- What is console.log printing and where are you actually getting redirected to? – Abdullah Jibaly Commented Sep 17, 2013 at 18:48
- Brett, Sorry, I can only give so much info because its related to an event registration and my boss is paranoid...but it's redirecting to a 404 on the same domain. However, I can go directly to test./promoCodeValue and that works. – Megan Taylor Commented Sep 17, 2013 at 19:10
- 1 Were you able to solve this problem? – Bhargav Nanekalva Commented Mar 29, 2015 at 11:07
2 Answers
Reset to default 1Demo jsFiddle
The following works fine for me, let me know if you need more.
HTML
<button id="promoCodeSubmit" onclick="ClickEvent()">Apply</button>
<input type="hidden" id="promoCodeValue" value="1"/>
JS
function ClickEvent(){
window.location.href='http://www.test.'+document.getElementById('promoCodeValue').value;
}
Why don't simplify your code and make it readable.It will also solve your problem. HTML
<button id="promoCodeSubmit" onclick="RedirectToLocation()">Apply</button>
<input id="promoCodeValue" type="text" value="testing123" />
Script:
function RedirectToLocation(){
window.location.href='http://www.test.'+document.getElementById('promoCodeValue').value;
}
Js Fiddle Demo
本文标签: javascriptButton onclick not redirecting to correct URLStack Overflow
版权声明:本文标题:javascript - Button onclick not redirecting to correct URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745187293a2646744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论