Skip to content
Advertisement

Current page URl check by JavaScript

I have tried to check the URL by this function. If we use single text then its working, but when we put the URL it’s not working.

jQuery(document).ready
    (
        function () 
        { 
            //var regExp = /franky/g; //It's working 
            var regExp = /http://localhost/sitename/members/g; //its not working 
            var testString = "http://localhost/sitename/members/alan/course/";//In your case it would be window.location;
            var testString =  window.location;//Inyour case it would be window.location;
            if(regExp.test(testString)) // This doesn't work, any suggestions.                 
            {                      
                alert("your url match");                 
            }else{
                alert("Not match");   
            }             
        }
    ); 

Advertisement

Answer

You mention the wrong regex in your code,

 var regExp = /http://localhost/sitename/members/g;

Here you will get a syntax error. Instead of this, you can use regex like,

 var regExp = new RegExp("http://localhost/sitename/members");

OR

 var regExp = /http://localhost/sitename/members/g;
Advertisement