Câu trả lời ban đầu
Một kiểm tra rất nhanh và rất linh hoạt là:fast and very flexible check is:
if (url.indexOf('://') > 0 || url.indexOf('//') === 0 ) { // URL is absolute; either "//example.com" or "//example.com" } else { // URL is relative }Điều này sẽ nhận ra một URL tuyệt đối, nếu:
- URL chứa ": //" bất cứ nơi nào sau ký tự đầu tiên, hoặc
- URL bắt đầu bằng "//" (tương đối giao thức)
- Không có regex.
- Không có jQuery hoặc phụ thuộc khác.
- Không có tên giao thức cứng làm cho trường hợp điều kiện nhạy cảm.
- Không có thao tác chuỗi (ví dụ: TolowerCase hoặc tương tự).
- Chỉ kiểm tra "tương đối hoặc tuyệt đối" nhưng không thực hiện bất kỳ kiểm tra tỉnh táo nào khác, mới có thể được sử dụng cho các URL web hoặc bất kỳ giao thức nội bộ nào.
Cập nhật 1 (ví dụ đầy đủ chức năng)
Dưới đây là một chức năng nhanh chóng trả về đúng/sai cho URL đã cho:function that returns true/false for the given URL:
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); }Và tương tự trong ES6:
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0)Cập nhật 2 (URL bên trong URL PARAM)
Để giải quyết thêm các URL ở định dạng /redirect?target=//example.org, tôi khuyên bạn nên sử dụng mã này:
function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative }Và tương tự ở dạng ngắn và es 6
// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false)Dưới đây là một số trường hợp thử nghiệm:
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> falseCập nhật 3 (làm rõ URL tương đối)
Tôi đã thấy một vài nhận xét về đầu ra không hợp lệ:
- Giải pháp trả về sai cho localhost
- Trả lời thất bại trên example.com
Tuy nhiên, những URL đó thực sự là URL tương đối. Thật dễ dàng để kiểm tra:those URLs are indeed relative URLs. It's easy to test:
- Tạo một số thư mục trên Webroot localhost của bạn, nói a/b/c/
- Tạo tệp index.html và vị trí sau liên kết vào đó: function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 0
- Mở trang chỉ mục trong trình duyệt của bạn: //localhost/a/b/c/index.html và nhấp vào liên kết. Bạn sẽ kết thúc trên // localhost/a/b/c/localhost (và không trên // localhost)
- Tương tự xảy ra khi đặt liên kết example.com vào tệp index.html của bạn. Bạn kết thúc trên //localhost/a/b/c/example.com thay vì //example.com
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Approach 1:
- Bàn luậnRegular Expression which checks if the URL contains “//” at a position in the URL .
- function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6/redirect?target=//example.org2.indexOf() method to get to know if the position of “://” has index greater than 0 or position of “//” has index equal to 0. Both these condition check leads us to the absolute URL.
Nhiệm vụ là kiểm tra xem URL được thông qua là tuyệt đối hay tương đối. Dưới đây là một vài cách tiếp cận: Cách tiếp cận 1:This example uses the approach discussed above.
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 2
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 7function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 1function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 4
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 1function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
Sử dụng biểu thức chính quy để kiểm tra xem URL có chứa /// ở vị trí trong URL không.
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 1function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 7function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
Ví dụ 1: Ví dụ này sử dụng phương pháp được thảo luận ở trên.
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 1 function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 2function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 4// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 2 // Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 5function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
/redirect?target=//example.org6/redirect?target=//example.org7
/redirect?target=//example.org8/redirect?target=//example.org9
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6/redirect?target=//example.org2function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 9 // Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 22
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3example.com2
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6localhost6function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3/redirect?target=//example.org2 /redirect?target=//example.org3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3/redirect?target=//example.org5
/redirect?target=//example.org6a/b/c/4
/redirect?target=//example.org8/redirect?target=//example.org9
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3localhost6 localhost7function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3localhost9__
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 1function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 06
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 08
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 10
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 11
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 12function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 13function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 14function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 15
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 17
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 19
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 21
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 22function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 23
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 25
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 22function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 27
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 29
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 29
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 1function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 2function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
Output:
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3/redirect?target=//example.org2 ________ 63 ________ 33 ________ 92 & nbsp;
Tiếp cận 2:This example uses the approach discussed above.
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 2
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 7function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 1function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 4
Sử dụng phương thức .indexof () để biết liệu vị trí của Hồi: // có chỉ số lớn hơn 0 hoặc vị trí của /// có chỉ số bằng 0. Cả hai điều kiện này kiểm tra dẫn chúng ta đến URL tuyệt đối.
Sử dụng biểu thức chính quy để kiểm tra xem URL có chứa /// ở vị trí trong URL không.
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 1function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 7function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
Ví dụ 1: Ví dụ này sử dụng phương pháp được thảo luận ở trên.
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 1 function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 2function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 4// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 2 // Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 5function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
/redirect?target=//example.org6// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 99function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6/redirect?target=//example.org2function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 9 // Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 22
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3example.com2
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6localhost6function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3/redirect?target=//example.org2 /redirect?target=//example.org3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3/redirect?target=//example.org5
/redirect?target=//example.org6const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 25
/redirect?target=//example.org8const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 27
/redirect?target=//example.org8/redirect?target=//example.org9
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6/redirect?target=//example.org2function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 1function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 06
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 08
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 43
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 45
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 46
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 12const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 48function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 14function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 15
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 3localhost6 localhost7function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 3localhost9__
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 4const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 54
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 22function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 23
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 25
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 22function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 27
// Test console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('//stackoverflow.com') ) // -> true console.log( isUrlAbsolute('stackoverflow.com') ) // -> false console.log( isUrlAbsolute('Ftp://example.net') ) // -> true console.log( isUrlAbsolute('/redirect?target=//example.org') ) // -> false 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 29
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 3function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 29
function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 9const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6function isUrlAbsolute(url) { if (url.indexOf('//') === 0) {return true;} // URL is protocol-relative (= absolute) if (url.indexOf('://') === -1) {return false;} // URL has no protocol (= relative) if (url.indexOf('.') === -1) {return false;} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST) if (url.indexOf('/') === -1) {return false;} // URL does not contain a single slash (= relative) if (url.indexOf(':') > url.indexOf('/')) {return false;} // The first colon comes after the first slash (= relative) if (url.indexOf('://') < url.indexOf('.')) {return true;} // Protocol is defined before first dot (= absolute) return false; // Anything else must be relative } 1function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6// Traditional JS, shortened function isUrlAbsolute(url) { return url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false; } // ES 6 const isUrlAbsolute = (url) => (url.indexOf('//') === 0 ? true : url.indexOf('://') === -1 ? false : url.indexOf('.') === -1 ? false : url.indexOf('/') === -1 ? false : url.indexOf(':') > url.indexOf('/') ? false : url.indexOf('://') < url.indexOf('.') ? true : false) 2function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
const isUrlAbsolute = (url) => (url.indexOf('://') > 0 || url.indexOf('//') === 0) 6function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 4function isUrlAbsolute(url) { return (url.indexOf('://') > 0 || url.indexOf('//') === 0); } 5
Output: