How to check if the url contains specific keyword in script include?

Thousif S N2
Tera Contributor

I want to check if  (URL = <keyword>){ then do this} .. so we already have a  script include? so i need add just one condition like this in the existing script?

4 REPLIES 4

Chuck Tomasi
Tera Patron

Check out the GlideURI class to investigate what's on the URI.

GlideURI - Scoped, Global | ServiceNow Developers

Hi Chuck,

 

Thank you for the reply.

But i am not able to achieve it on my script. So can you just give me a piece of example code to check whether url has keyword ? 

 

Can you share what you have tried?

An example of using GlideURI to determine if a parameter is available and has a value is with the get() method.

var gURI = new GlideURI();
gURI.set('sysparm_query', 'priority=2^active=true' );
var fileString = gURI.get('sysparm_query');
gs.info(fileString);

If you want the entire URI, then use toString() on the gURI (see toString() in the linked doc above.) You can then use the standard JavaScript indexOf() method to determine if the URI contains your string somewhere.

var gURI = new GlideURI();
var myURI = gURI.toString();
if (myURI.indexOf('Chuck') != -1) {
   gs.info('URI contains Chuck');
} else {
   gs.info('URI does not contain Chuck');
}

Gaurav Vaze
Kilo Sage

If you are using client script only, then top.location.href can be used(this is to be used with onload client script)