简体   繁体   中英

How do I pass multiple variables through a URL in Javascript

Folks,

First question here and trying to teach myself js and jquery, please forgive (or correct) any errors in nomenclature....

I am happily managing to pass through a variable from one page to another with this piece of code:

function openPage() {window.open('datapage_test.html?id=' + dataset[0].Var_Name)}

returns: file:///*path*/datapage_test.html?id=UK_Wakefield

When I try and extend this to pass multiple variables through I fail:

function openPage() {
    window.open('datapage_test.html?id=' + dataset[0].Var_Name + dataset[0].Var_Class + dataset[0].Var_Instance )
}

returns: file:///C:/*path*/datapage_test.html?id=WakefieldundefinedUK

All pointers gratefully received. J

Urls are configure like this:

http://www.example.com/images/assets/file.html?id=1&type=2
protocol://domainName.com/path/to/myFile.html?parameter=value&other=value

This is your important part you are trying to do ?parameter=value&other=value

You can try something like this:

  function openPage() {window.open('datapage_test.html?id=' + dataset[0].Var_Name +'&val2=' + dataset[0].Var_Class +'&val3=' + dataset[0].Var_Instance )}

Hope this helps :)

Starting variables in urls is done with ? which is what you are doing, however, for variables after that, you use & . For example, if I have the variables cat = meow and dog = woof, you could do http://example.com/?cat=meow&dog=woof .

Also, might want to check dataset[0].Var_Class . Apparently it's undefined in your example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM