javascript request url code example
Example 1: javascript get url parameters
let url = 'https://www.example.com?name=n1&name=n2';
let params = (new URL(url)).searchParams;
params.get('name')
params.getAll('name')
Example 2: how to get browser url in javascript
function getURL() {
alert("The URL of this page is: " + window.location.href);
}
Example 3: create http request javascript
const Http = new XMLHttpRequest();
const url='https://jsonplaceholder.typicode.com/posts';
Http.open("GET", url);
Http.send();
Http.onreadystatechange = (e) => {
console.log(Http.responseText)
}
Example 4: GET req with js
function httpGet(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}