0

So i want to be redirected to a certain page in a website but I want to be able to be redirected at any point during my navigation through the site. For example I am at mySite.com/ and want to add parameters myParam=1 and othParam=2

so pattern would be mySite.com/?myParam=1&othParam=2

but my trouble is when mySite.com/ has pre-existing parameters like mySite.com/?pre=4&user=14 because then I am unable to redirect to mySite.com/?pre=4&user=14?myParam=1&othParam=2

so I am trying to create a pattern like

$(page I am on)?myParam=$(value1)&othParam=$(value2)

However trouble is seen when page I am on also has parameters.

Does anybody has a solution or can help?

Patrick Schocke
  • 1,410
  • 1
  • 9
  • 21
M.Mena
  • 1

2 Answers2

0

Welcome to the community..! M.Mena,

Try this Demo

Here the trick get the url and then append it with the value parameter you want to achieve.

Jquery

var urlLocation = window.location;

var value1 = 'Alex';

var value2 = 'Pizza';

var ma = urlLocation + '?myParam='+ value1 +'&othParam=' + value2;

$('p').html(ma);

If if there is already a parameter and you want to reset it

then you need remove the url last part, after that append it with the new parameter that you want to assigned

Check this DEMO

Jquery

var urlLocation = 'https://www.google.com/?fifa-world-cup';

var regex = new RegExp('/[^/]*$');

var last = urlLocation.replace(regex, '/') // remove

var value1 = 'Alex';

var value2 = 'Pizza';

var ma = last + '?myParam='+ value1 +'&othParam=' + value2; // append

$('.original').html(urlLocation);
$('.test').html(last);
$('.test1').html(ma);

Thanks and have a nice day

Fiido93
  • 1,694
  • 1
  • 12
  • 20
  • As I understood the OP correctily he wan't to keep the original url parameter and appends his new to the old. As your demo shows you are deleting the old parameters – Patrick Schocke Oct 27 '18 at 06:57
  • He says here trying to create a pattern like etc. Then he add the url that user got right now has parameter. What do you mean? by appends his new to the old. Obviously he trying to create a new pattern. – Fiido93 Oct 27 '18 at 07:03
  • Yes I want to add parameters to the old parameters, if the site I am on has parameters. There are different pages on the site where parameters exist and do not exist, so it is difficult to create a pattern because the pattern would be either (original)?parmam1=(alex)&param2=(joe) or (original)&parmam1=(alex)&(param). See how in some cases it would fail on different pages of the site? Is there a fix? Thanks in advance. – M.Mena Oct 29 '18 at 16:03
0

This function appends a value to the existing url or, if the parameter is allready existing, changes the value.

Change url with window.location!

the function updateQueryStringParameter(uri, keys, values) takes keys and values as an array so you can append multiple parameters at once.

var url = 'https://stackoverflow.com/questions/53018391/how-to-create-a-redirect-pattern-for-url/53019364?noredirect=1';

function updateQueryStringParameter(uri, keys, values) {
  for(var x = 0; x < keys.length; x++) {
    var key = keys[x];
    var value = values[x];
    var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
    var separator = uri.indexOf('?') !== -1 ? "&" : "?";
    if (uri.match(re)) {
      uri =  uri.replace(re, '$1' + key + "=" + value + '$2');
    }
    else {
      uri =  uri + separator + key + "=" + value;
    }
  }
  return uri;
}

$('p').html(updateQueryStringParameter(url, ['eat', 'play'], ['pizza', 'football']));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>

part of the function found here.

Patrick Schocke
  • 1,410
  • 1
  • 9
  • 21