TMeyer Posted February 26, 2017 Report Share Posted February 26, 2017 I have the following code in the footer and it produces a button on the bottom of my results page that works to redirect the user to another url. <script type="text/javascript"> if(!document.getElementById("norecord")) { document.write('<button onclick=document.location.href="https://www.myirlog.com/logbook/new-record-entry">No, Really, I want to add a new record</button>'); } </script> However, when I put that same code inside of an HTML block, it does not work: <script type="text/javascript"> if('[@field:participant_1_name]' != '[@authfield:Name]' && '[@field:participant_2_name]' != '[@authfield:Name]' && '[@field:participant_3_name]' != '[@authfield:Name]' ) { if('[@field:participant_2_name]' == '' || '[@field:participant_3_name]' == '' ) { document.write('<button onclick=document.location.href="https://www.google.com">take me away</button>'); } } </script> The buttons appear in both locations, but only the footer button works, when the in-line button (the one coded in the HTML block) is clicked, the whole page just refreshes instead of going to google. Quote Link to comment Share on other sites More sharing options...
Mathilda Posted March 17, 2017 Report Share Posted March 17, 2017 On 2/26/2017 at 0:58 PM, TMeyer said: I have the following code in the footer and it produces a button on the bottom of my results page that works to redirect the user to another url. <script type="text/javascript"> if(!document.getElementById("norecord")) { document.write('<button onclick=document.location.href="https://www.myirlog.com/logbook/new-record-entry">No, Really, I want to add a new record</button>'); } </script> However, when I put that same code inside of an HTML block, it does not work: <script type="text/javascript"> if('[@field:participant_1_name]' != '[@authfield:Name]' && '[@field:participant_2_name]' != '[@authfield:Name]' && '[@field:participant_3_name]' != '[@authfield:Name]' ) { if('[@field:participant_2_name]' == '' || '[@field:participant_3_name]' == '' ) { document.write('<button onclick=document.location.href="https://www.google.com">take me away</button>'); } } </script> The buttons appear in both locations, but only the footer button works, when the in-line button (the one coded in the HTML block) is clicked, the whole page just refreshes instead of going to google. Try using my code instead of document.write part <script type="text/javascript"> var uniqueId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); document.write('<button id="'+ uniqueId +'">take me away</button>'); var button = document.getElementById(uniqueId.toString()); if(button){ button.addEventListener('click', function(event){ event = event || window.event; document.location.href = "https://google.com"; event.stopPropagation(); event.cancelBubble = true; event.returnValue = false; return false; }, false); } </script> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.