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
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

本文标签: javascriptButton onclick not redirecting to correct URLStack Overflow