Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add a trailing slash javascript url

// Add trailing slash even when a query is present.
// supported in all modern browsers
let url = new URL("a?foo=bar", "https://example.com");

if (url.pathname[url.pathname.length - 1] != "/") {
	url.pathname += "/";
}

// https://example.com/a/?foo=bar
console.log(url.toString());
Comment

js add trailing slash to url (if not present)

var lastChar = url.substr(-1); // Selects the last character
if (lastChar != '/') {         // If the last character is not a slash
   url = url + '/';            // Append a slash to it.
}

// The temporary variable name can be omitted, and directly embedded in the assertion:

if (url.substr(-1) != '/') url += '/';

// Since the goal is changing the url with a one-liner, the following solution can also be used:

url = url.replace(//?$/, '/');
// If the trailing slash exists, it is replaced with /.
// If the trailing slash does not exist, a / is appended to the end (to be exact: The trailing anchor is replaced with /).
Comment

PREVIOUS NEXT
Code Example
Javascript :: node load file 
Javascript :: js compare 2 arrays for unique values 
Javascript :: jquery on click 
Javascript :: js upload file dialog 
Javascript :: useFetch custom hook for React 
Javascript :: req.body empty mongodb 
Javascript :: js string find regex 
Javascript :: Jquery get value of dropdown selected 
Javascript :: placeholder in angular 9 select with working required 
Javascript :: javascript find smallest number in an array 
Javascript :: phone number validation regex 
Javascript :: jquery delay to call function 
Javascript :: make js file windows command 
Javascript :: first x characters of string javascript 
Javascript :: check either define or undefined in javascript 
Javascript :: type numeric value only in textbox javascript 
Javascript :: uppercase and lowercase letters in js 
Javascript :: nodejs check if variable is undefined 
Javascript :: Selector All Element querySelector Method 
Javascript :: fb login npm 
Javascript :: generate random color jquery 
Javascript :: generate unique id javascript 
Javascript :: javascript escape html 
Javascript :: javascript object total 
Javascript :: javascript location redirect 
Javascript :: convert moment date to utc format moment 
Javascript :: iso date javascript 
Javascript :: js is number 
Javascript :: how to access model data in jsp spring mvc 
Javascript :: jquery button text 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =