Hướng dẫn javascript check if url is absolute or relative - javascript kiểm tra xem url là tuyệt đối hay tương đối

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') ) // -> false

Cậ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:

  1. Tạo một số thư mục trên Webroot localhost của bạn, nói a/b/c/
  2. 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
  3. 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)
  4. 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
  • Bàn luận
  • 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 .
    • 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;

      • 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.

      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:


    Làm thế nào để bạn biết nếu một URL là tương đối hay tuyệt đối?

    Câu trả lời ban đầu. Đ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)URL contains "://" anywhere after the first character, or. URL starts with "//" (protocol relative)

    URL có tuyệt đối không?

    Một URL tuyệt đối chứa tất cả các thông tin cần thiết để xác định vị trí tài nguyên.Một URL tương đối định vị một tài nguyên sử dụng URL tuyệt đối làm điểm bắt đầu.Trong thực tế, "URL hoàn chỉnh" của mục tiêu được chỉ định bằng cách kết hợp các URL tuyệt đối và tương đối.. A relative URL locates a resource using an absolute URL as a starting point. In effect, the "complete URL" of the target is specified by concatenating the absolute and relative URLs.

    Làm thế nào để bạn kiểm tra xem một chuỗi là một URL JavaScript hợp lệ?

    Phương thức htmlinputEuity.checkValiation () được sử dụng để kiểm tra xem một chuỗi trong thuộc tính giá trị của phần tử là URL.Phương thức kiểm tra () trả về đúng nếu giá trị là URL thích hợp và sai nếu đầu vào không phải là URL thích hợp. checkValidity() method is used to check if a string in element's value attribute is URL . The checkvalidity() method returns true if the value is a proper URL and false if the input is not a proper URL.

    URL tuyệt đối có tốt hơn tương đối không?

    Một URL tuyệt đối chứa nhiều thông tin hơn một URL tương đối.URL tương đối thuận tiện hơn vì chúng ngắn hơn và thường di động hơn.Tuy nhiên, bạn chỉ có thể sử dụng chúng để tham chiếu các liên kết trên cùng một máy chủ với trang có chứa chúng.. Relative URLs are more convenient because they are shorter and often more portable. However, you can use them only to reference links on the same server as the page that contains them.

    Chủ đề