javascript - Button onclick not redirecting to correct URL - Stack Overflow

<button id="promoCodeSubmit" onclick="window.location.href=''+document.getEl

<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
Add a ment  | 

2 Answers 2

Reset to default 1

Demo 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745187293a4615697.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信