September 1, 200817 yr Hi, Probably not the first time this has been posted, but I get feeling this is slightly different from the usual expression question (but maybe not)... heres the deal... window.onload = checksite; function checksite(){ var setit = top.location; function checkSsn(chknow){ var checking = "/*http:\/\/www.were.co.uk$/"; if (checking.test(chknow)) { alert ("works"); }else{ alert ("failed"); } } checkSsn(setit); } I need to get a regular expression to check the "domain name" of the variable "siteloc". say siteloc returns.... http://www.were.co.uk/somefile.php (not real address).. I need a regular expression to check if the beginning of this string is "http://www.were.co.uk" , followed by anything after. As soon as I add the following line... the script refuses to work and I cannot figuire out why!! The killer: if (checking.test(chknow)) { alert ("works"); }else{ alert ("failed"); } Can anyone shed some light for me please? Thanks on advance
September 1, 200817 yr Author also debugging says.... object doesn't support this property or method...?
September 2, 200817 yr You need to instantiate the variable called checking as a RegExp type variable: var checking = new RegExp("/*http:\/\/www.were.co.uk$/"); Should work after that. The error message gives it away... Because you've just declared checking as a normal var, it does not have a test() method.
September 2, 200817 yr Author thanks leesalter and dizi.... Ok so the javascript does work now - however it is constantly returning "Failed" alert, meaning that the regular expression itself is not correct... Anyone know what I'm doing wrong again? window.onload = checksite; function checksite(){ var setit = top.location; function checkSsn(chknow){ var checking = new RegExp("/*http:\/\/www\.were\.co\.uk$/"); if (checking.test(chknow)) { alert ("works"); }else{ alert ("failed"); } } checkSsn(setit); }
September 2, 200817 yr Regular Expressions are definitely not a strong point of mine, but you could try:- var checking = new RegExp('^http:\/\/www\.were\.co\.uk\/*')
September 2, 200817 yr Author weey. Ive been trying ever since, and couldn't get it to work!... but that worked, thanks. I have 1 or 2 more bits I need to add to it, but I think I can handle this bit!! Again thanks for you help, much appreciated!
September 2, 200817 yr Author LeeSalter, again thanks for you help, I have now 100% completed my script (for now). IF you are curious and would like to know what I used it for PM me, and i'll send you a link
September 2, 200817 yr No problem, webhost. Good to see that my 5 years of C#/ASP.NET/Web App development hasn't been entirely wasted! :-)
Create an account or sign in to comment