This is an old revision of the document!


Javascript: Browser URL mit GET Parametern ändern

Dieser Code liest die Browser URL aus, trennt sie in Ihre Komponenten Server Host, Pfad/Path und GET Parametern. Einer der GET-Parameter wird neu gesetzt und die aktualisierte URL wird aufgerufen.

	const url_host          = window.location.origin;            // https://www.domain.com/
	const url_path          = window.location.pathname;          // folder/subpage.php/
	const url_search_string = window.location.search;            // ?parameter=true&other_param=1024
	const url_params_object = new URLSearchParams( url_search ); // converts params to manipulatable object
	      url_params_object.set( 'other_param', 'new_value');    // change a parameter
	const url_params_string = url_params.toString();             // get paramaters as string
	const new_url           = url_host + url_path + '?' + url_params_string;
 
	window.location.replace( new_url );

Hinweis: nicht IE11 kompatibel, wegen URLSearchParams()


Page Tools