0

Here is my html tag with href

<a href='components/home/singleActivity.html?view="search"'>
....
</a>

I want to send a string parameter, is it the correct way ?

how to retrieve that parameter in javascript ? and how to send 2 parameters for the same href ?

spanpa
  • 1
  • 1
  • 1
  • 6

4 Answers4

3

It should be without quotes components/home/singleActivity.html?view=search to send two parameters join them with &

components/home/singleActivity.html?foo=bar&baz=quux

to read them in javascript use this code:

var params = {};
location.search.slice(1).split("&").forEach(function(pair) {
   pair = pair.split("=");
   params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
});
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • 1
    On new browser you can also use the URL object wich makes thinks 10x easier. – Bruno Souza Oct 07 '19 at 15:31
  • @BrunoSouza I don't think you can get all keys using URL object you can only get value bar and quux if you know foo and baz. See [How can I get query string values in JavaScript?](https://stackoverflow.com/q/901115/387194) – jcubic Oct 07 '19 at 19:56
0

Try

<a href='components/home/singleActivity.html?view=Search&test2=New'>
MyLink
</a>
Waruna Manjula
  • 3,067
  • 1
  • 34
  • 33
0

In order to send the parameters in the URL you donot need to enclose the value inside quote('') or doublequotes(""). You just need to send the values (or the multiple values) as shown below..

components/home/singleActivity.html?view=search&secondvalue=anythingthatyouthinkof

Also keep in mind that you need to keep track of the urlencoding.

And for retrieving the parameters this thread explains it in great detail.

Community
  • 1
  • 1
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
0

To send multiple values you can use ?view=search&key2=value2&key3=value3 and so on.

Moreover to access these parameters you can access the URL using window.location.

Something like

var params = window.location.split('?')[1];

This will give you everything after the ? in the URL.

Vinod Bhavnani
  • 2,185
  • 1
  • 16
  • 19