Hướng dẫn what is url in javascript? - url trong javascript là gì?

Giao diện URL được sử dụng để phân tích, xây dựng, bình thường hóa và mã hóa URL. Nó hoạt động bằng cách cung cấp các thuộc tính cho phép bạn dễ dàng đọc và sửa đổi các thành phần của URL.URL interface is used to parse, construct, normalize, and encode URLs. It works by providing properties which allow you to easily read and modify the components of a URL.

Bạn thường tạo một đối tượng URL mới bằng cách chỉ định URL dưới dạng chuỗi khi gọi hàm tạo của nó hoặc bằng cách cung cấp URL tương đối và URL cơ sở. Sau đó, bạn có thể dễ dàng đọc các thành phần phân tích cú pháp của URL hoặc thay đổi URL.

Nếu trình duyệt chưa hỗ trợ hàm tạo URL(), bạn có thể truy cập đối tượng URL bằng thuộc tính URL của giao diện Window. Hãy chắc chắn kiểm tra xem liệu có bất kỳ trình duyệt mục tiêu nào của bạn yêu cầu điều này được đặt trước không.

Lưu ý: Tính năng này có sẵn trong nhân viên web This feature is available in Web Workers

Người xây dựng

URL()

Tạo và trả về một đối tượng URL tham chiếu URL được chỉ định bằng chuỗi URL tuyệt đối hoặc chuỗi URL tương đối và chuỗi URL cơ sở.

Thuộc tính thể hiện

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
2

Một chuỗi chứa

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
3 theo sau là định danh đoạn của URL.

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
4

Một chuỗi chứa miền (đó là tên máy chủ) theo sau (nếu một cổng được chỉ định) A

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
5 và cổng của URL.

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
6

Một chuỗi chứa miền của URL.

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
7

Một trình chuỗi biến đổi một chuỗi chứa toàn bộ URL.

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
8 Chỉ đọcRead only

Trả về một chuỗi chứa nguồn gốc của URL, đó là sơ đồ của nó, miền và cổng của nó.

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
9

Một chuỗi chứa mật khẩu được chỉ định trước tên miền.

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
0

Một chuỗi chứa

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
1 ban đầu theo sau là đường dẫn của URL, không bao gồm chuỗi truy vấn hoặc đoạn.

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
2

Một chuỗi chứa số cổng của URL.

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
3

Một chuỗi chứa sơ đồ giao thức của URL, bao gồm cả

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
5 cuối cùng.

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
5

Một chuỗi chỉ ra chuỗi tham số của URL; Nếu bất kỳ tham số nào được cung cấp, chuỗi này bao gồm tất cả chúng, bắt đầu bằng ký tự

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
6 hàng đầu.

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
7 Chỉ đọcRead only

Một đối tượng

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
8 có thể được sử dụng để truy cập các tham số truy vấn riêng lẻ được tìm thấy trong
url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
5.

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
0

Một chuỗi chứa tên người dùng được chỉ định trước tên miền.

Phương pháp tĩnh

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
1

Trả về một chuỗi chứa một url blob duy nhất, đó là một URL có

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
2 làm sơ đồ của nó, tiếp theo là một chuỗi mờ xác định duy nhất đối tượng trong trình duyệt.

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
3

Thu hồi một URL đối tượng được tạo trước đó bằng

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
4.

Phương pháp thể hiện

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
5

Trả về một chuỗi chứa toàn bộ URL. Nó là một từ đồng nghĩa với

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
6, mặc dù nó không thể được sử dụng để sửa đổi giá trị.

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
7

Trả về một chuỗi chứa toàn bộ URL. Nó trả về cùng một chuỗi với thuộc tính

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
7.

Ghi chú sử dụng

Trình xây dựng có tham số

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
9 và tham số
const response = await fetch(new URL('http://www.example.com/démonstration.html'));
0 tùy chọn để sử dụng làm cơ sở nếu tham số
// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
9 là URL tương đối:

const url = new URL('../cats', 'http://www.example.com/dogs');
console.log(url.hostname); // "www.example.com"
console.log(url.pathname); // "/cats"

Các thuộc tính URL có thể được đặt để xây dựng URL:

url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"

URL được mã hóa theo các quy tắc được tìm thấy trong RFC 3986. Ví dụ:

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"

Giao diện

url.pathname = 'démonstration.html';
console.log(url.href); // "http://www.example.com/d%C3%A9monstration.html"
8 có thể được sử dụng để xây dựng và thao tác chuỗi truy vấn URL.

Để lấy các thông số tìm kiếm từ URL của cửa sổ hiện tại, bạn có thể làm điều này:

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"

Phương thức

// https://some.site/?id=123
const parsedUrl = new URL(window.location.href);
console.log(parsedUrl.searchParams.get("id")); // "123"
5 của URL chỉ trả về giá trị của thuộc tính
url.hash = 'tabby';
console.log(url.href); // "http://www.example.com/cats#tabby"
7, do đó hàm tạo có thể được sử dụng để bình thường hóa và mã hóa trực tiếp URL.

const response = await fetch(new URL('http://www.example.com/démonstration.html'));

Thông số kỹ thuật

Sự chỉ rõ
URL Tiêu chuẩn # API
# api

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Làm thế nào để bạn tạo một URL trong JavaScript?

Cú pháp để tạo một đối tượng URL mới:..
URL mới (url, [cơ sở]).
Đặt url = url mới ('https://javascript.info/profile/admin') ;.
URL mới ('https://google.com/search? Truy vấn = javaScript').

Có gì trong URL?

URL chứa tên của giao thức cần thiết để truy cập tài nguyên, cũng như tên tài nguyên.Phần đầu tiên của URL xác định giao thức nào sẽ sử dụng làm phương tiện truy cập chính.Phần thứ hai xác định địa chỉ IP hoặc tên miền - và có thể là tên miền phụ - nơi có tài nguyên.the name of the protocol needed to access a resource, as well as a resource name. The first part of a URL identifies what protocol to use as the primary access medium. The second part identifies the IP address or domain name -- and possibly subdomain -- where the resource is located.

URL trong nút JS là gì?

Mô -đun Node: URL cung cấp hai API để làm việc với các URL: API cũ là Node.js cụ thể và API mới hơn thực hiện cùng tiêu chuẩn URL WhatWG được sử dụng bởi các trình duyệt web.Một so sánh giữa API WhatWG và Legacy được cung cấp dưới đây.a legacy API that is Node. js specific, and a newer API that implements the same WHATWG URL Standard used by web browsers. A comparison between the WHATWG and Legacy APIs is provided below.

URL trong API là gì?

API URL là một thành phần của tiêu chuẩn URL, xác định những gì cấu thành một trình định vị tài nguyên thống nhất hợp lệ và API truy cập và thao tác URL.a component of the URL standard, which defines what constitutes a valid Uniform Resource Locator and the API that accesses and manipulates URLs.