v2.0.3

URL Extensions

Last updated


Request, history, and query-string methods RenderJS adds to URL.prototype.

URL


Source: src/prototype/URL.ts
Method / PropertyDescription
url.$queryAllQuery string parsed into a plain object
url.$imagePromise resolving a preloaded Image for the URL
url.$$200(isImg = false)Fetches the URL; if the response is an image, converts it to an object-URL blob ({ src, img }) unless it's SVG and isImg is falsy
url.$req(body, once = false)Fetch wrapper: method (default GET), header, and credentials: true for "include" (otherwise "same-origin"); body.json is sent as JSON, or with body.files + body.tag as FormData (JSON keys appended as fields); Content-Type: application/json is added whenever body.files is absent; resolves with parsed JSON or text by response Content-Type; rejects with the response text on 4xx/5xx. With once: true, a call made while the same URL is still in flight (tracked in a shared Map) returns a promise that never settles; body: null returns undefined
url.$$query(key)searchParams.get(key)
url._history(title?)pushState to this URL, records it in the in-memory history stack, optionally sets document.title
url._query(obj)Returns a new URL with obj's keys merged into the existing query string
url.__history(title?)replaceState to this URL (also updates the history stack)
url.__query(obj)Returns a new URL with the query string replaced entirely by obj
url.query_(keys)Returns a new URL with the given key (or array of keys) removed from the query string
url.query__()Returns a new URL with the query string cleared

Example


Query-string methods return a new URL; $req parses the response by Content-Type and rejects with the response text on 4xx/5xx. The preview runs in an about:srcdoc frame where location.origin is null, so it resolves paths with document.baseURI; on a normal page "/llms.txt".$req() works directly.
  1. <script>
  2. const url = new URL("https://renderjs.pardn.io/docs?page=2&lang=en");
  3. console.log(url.$queryAll, url.$$query("page"));
  4. console.log(url._query({ sort: "asc" }).href);
  5. console.log(url.__query({ q: "dom" }).href);
  6. console.log(url.query_("lang").href);
  7. console.log(url.query__().href);
  8. new URL("/llms.txt", document.baseURI).$req()
  9. .then((text) => console.log(text.split("\n")[0]))
  10. .catch((err) => console.error(err));
  11. new URL("/llms.txt", document.baseURI).$req({
  12. method: "POST",
  13. json: { key: "value" },
  14. header: { "X-Token": "..." }
  15. }).catch((err) => console.error("rejected:", String(err).length, "chars of response text"));
  16. </script>
Live preview · @pardnchiu/renderjs@2.0.3
中文