URL creation: Client script

lomouhamadou
Kilo Guru

Hi all,

I need your help once again.

I have this client script on HR case that check if an URL is added in the additional comment. If yes, then create the hyperlink.

However when the link is "www.google.be" then cae is submitted but link is string and when the url contains "http(s)" then client script give an error.

Any idea to help on this ?

function onSubmit() {

      // check comments and add href

      var new_comments = g_form.getValue('comments')+'';

      if(new_comments!=''){

              g_form.setValue('comments', urlify(new_comments));

      }

      // check notes and add href

      var new_notes = g_form.getValue('work_notes')+'';

      if(new_notes!=''){

              g_form.setValue('work_notes', urlify(new_notes));

             

      }

}

// function to create link

function urlify(text) {

      var urlRegex = /(https?:\/\/[^\s]+)/g;

      return text.replace(urlRegex, function(url) {

              alert($_URI);

              return '[code]<p><a class="web" target="_blank" href="' + url + '">' + url + '</a></p>[/code]';

      });

}

1 ACCEPTED SOLUTION

well, there is only one place where you use the URI... the alert. I assume the alert is there only for debug purposes. What happens if you remove/comment that line?



The $-notation seems to be some jQuery syntax which would trigger a best practice violation as well. You should work with g_form.getValue / g_form.getDisplayValue instead.


View solution in original post

5 REPLIES 5

Daniel Draes
ServiceNow Employee
ServiceNow Employee

What kind of error do you receive? Have you checked the javascript console of your browser?



In any case, why render that on client side and not as a business rule on server side?


I tried to add the link http://google.be



find_real_file.png



onSubmit script error: ReferenceError: $_URI is not defined:
function onSubmit() {
var new_comments = g_form.getValue('comments')+'';
if(new_comments!=''){
  g_form.setValue('comments', urlify(new_comments));
}
var new_notes = g_form.getValue('work_notes')+'';
if(new_notes!=''){
  g_form.setValue('work_notes', urlify(new_notes));
 
}
}


well, there is only one place where you use the URI... the alert. I assume the alert is there only for debug purposes. What happens if you remove/comment that line?



The $-notation seems to be some jQuery syntax which would trigger a best practice violation as well. You should work with g_form.getValue / g_form.getDisplayValue instead.


Yeah, either comment out the alert line or define/assign $_URI before you use it.