Jump to content

Redirect works in footer but not html block


Recommended Posts

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. 

Link to comment
Share on other sites

  • 3 weeks later...
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>

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...