How do I set up a hyperlink that will not ask the user to navigate away from the page?

Patrick Schult2
Giga Guru

My use case is this - I will have a macro displayed on an Incident form, and the macro will have an <a> tag, with hyperlink. Now, the hyperlink is a "tel:{phonenumber}" style link, intended to launch Skype or whatever to place a phone call.

The link works great - except for when you have entered data into the form, and NOT saved. When you click the link, the browser thinks you are navigating away, and thus asks if you really want to proceed. I want it to NOT do this, because "tel:" links would not actually send the browser anywhere, so the message is misleading.

Any ideas? I can share some screenshots if that doesn't make sense.

1 ACCEPTED SOLUTION

Mwatkins
ServiceNow Employee
ServiceNow Employee

The message that is being thrown is coming from the onbeforeunload event. You can bypass it for just "tel:" links by making a UI Macro like this.


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


<script>


  jQuery(function($)


  {


          //store onbeforeunload for later use


      $(window).data('beforeunload',window.onbeforeunload);  




          //remove||re-assign onbeforeunload on hover


      $('a[href^="tel:"]')


          .hover(


                        function(){window.onbeforeunload=null;},


                        function(){window.onbeforeunload=$(window).data('beforeunload');}


                      );




  }


);


</script>


  <p>Just call us at <a class="cssclassname" href="tel:18005555555"


title="CLICK TO DIAL - Mobile Only">(800) 555-5555</a>.</p>


</j:jelly>


View solution in original post

4 REPLIES 4

Mwatkins
ServiceNow Employee
ServiceNow Employee

The message that is being thrown is coming from the onbeforeunload event. You can bypass it for just "tel:" links by making a UI Macro like this.


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


<script>


  jQuery(function($)


  {


          //store onbeforeunload for later use


      $(window).data('beforeunload',window.onbeforeunload);  




          //remove||re-assign onbeforeunload on hover


      $('a[href^="tel:"]')


          .hover(


                        function(){window.onbeforeunload=null;},


                        function(){window.onbeforeunload=$(window).data('beforeunload');}


                      );




  }


);


</script>


  <p>Just call us at <a class="cssclassname" href="tel:18005555555"


title="CLICK TO DIAL - Mobile Only">(800) 555-5555</a>.</p>


</j:jelly>


That's a really great solution - I never would have thought of that! I pasted that snippet into my macro and it works great.


Julian Hoch
ServiceNow Employee
ServiceNow Employee

Can't you just put a target="_blank" in the hyperlink so it won't be opened in the same context?


Mwatkins
ServiceNow Employee
ServiceNow Employee

target="_blank" will avoid the onbeforeunload event but it will open a new tab with the address tel:555-555-5555 and then trigger a telephone app. If you don't mind the extra tab getting opened then that will work too.