Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  116] [ 6]  / answers: 1 / hits: 92417  / 12 Years ago, mon, may 21, 2012, 12:00:00

How can I test a URL if it is a relative or absolute path in Javascript or jQuery? I want to handle accordingly depending if the passed in URL is a local or external path.



if (urlString starts with http:// or https://)
//do this

More From » jquery

 Answers
19
var pat = /^https?:///i;
if (pat.test(urlString))
{
//do stuff
}


For protocol relative urls, use this regex:



/^https?://|^///i


[#85452] Saturday, May 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ernest

Total Points: 332
Total Questions: 92
Total Answers: 98

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
;