Commit a94f1fa1 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Robert Lyon

Bug 1852026: refactor getUrlParameter(param, url)

to use JS function URLSearchParams. The result should stay the same, it should not change any behaviour.

The function is used in :
./htdocs/relatedtags.php
./htdocs/tags.php
./htdocs/js/adminusersearch.js
./htdocs/js/export.js
./htdocs/js/privacy.js
./htdocs/js/adminexportqueue.js
./htdocs/artefact/annotation/blocktype/annotation/js/annotation.js
./htdocs/artefact/plans/js/planview.js
./htdocs/artefact/peerassessment/blocktype/peerassessment/js/peerassessment.js
./htdocs/artefact/file/js/filebrowser.js
./htdocs/theme/raw/js/formtabs.js
./htdocs/view/choosetemplate.php

the function updateUrlParameter is used in:
./htdocs/relatedtags.php
./htdocs/js/privacy.js
./htdocs/js/mahara.js
./htdocs/artefact/file/js/filebrowser.js

behatnotneeded

Change-Id: I1593f59624a5de3f90054a702d4e94dd7029f321
parent e660923d
......@@ -980,24 +980,17 @@ function fetch_graph_data(opts) {
* Allow the finding / changing of a param from a url string
*/
function updateUrlParameter(url, param, value) {
var found = false;
var vars = url.split("?");
if (typeof(vars[1]) !== 'undefined') {
varparams = vars[1].split("&");
for (var i = 0; i < varparams.length; i++) {
var pair = varparams[i].split("=");
if (pair[0] == param) {
pair[1] = value;
found = true;
}
varparams[i] = pair.join("=");
}
vars[1] = varparams.join("&");
var searchParams = new URLSearchParams(vars[1]);
// if param exists in url, replace it and delete duplicates
// otherwise create it and add to end of url string
searchParams.set(param, value);
vars[1] = searchParams.toString();
url = vars.join("?");
if (!found) {
url = url + '&' + param + '=' + value;
}
}
else {
url = url + '?' + param + '=' + value;
......@@ -1005,7 +998,14 @@ function updateUrlParameter(url, param, value) {
return url;
}
function getUrlParameter(param, url) {
/*
* Gets a param value from a url
* @param string param the name of the parameter
* @param string url the url that could contain the param
* @param boolean all true to return an array with all values
*/
function getUrlParameter(param, url, all) {
if (!url) {
url = window.location.href;
}
......@@ -1013,12 +1013,13 @@ function getUrlParameter(param, url) {
if (!vars[1]) return null; // no search parameters - are you using clean URLs?
varparams = vars[1].split("&");
for (var i = 0; i < varparams.length; i++) {
var pair = varparams[i].split("=");
if (pair[0] == param) {
return pair[1] || '';
var searchParams = new URLSearchParams(vars[1]);
if (searchParams.has(param)) {
if (typeof(all) != 'undefined' && all == true) {
return searchParams.getAll(param) || [];
}
else {
return searchParams.get(param) || '';
}
}
return null;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment