0

Is it possible to pass two variables through to another html page through a link?

For example... I have a directory page where when a user clicks a store link, it links to another html page with the map centered on the specific store. The function that centers the map on the store takes two variables, storeID, storeName. There are hundreds of stores and currently I would have to manually create each store page separately by hardcoding those two variables so that each page loads slightly differently. Is there a way to pass these two variables with a link, to avoid many different html pages?

Perhaps something along the lines of <a href "thisPage.html", var1, var2>?

ex. code

thispage.html

var1;
var2; 

function myFunc(var1, var2) {
    ~~~
}
2
  • You can use the hash portion of the URL to set and get variables you are going to use in javascript. Commented Aug 10, 2012 at 18:26
  • Is the map the only thing that is different on the store page? I'd expect to see an address and opening hours too. Commented Aug 10, 2012 at 18:28

2 Answers 2

1

Not like that, but you can use URL parameters.

thisPage.html?var1=foo&var2=bar

Then you can read them on the second page. I like to use this function for that:

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

From http://www.netlobo.com/url_query_string_javascript.html

So on your second page, you can just do gup("var1") and you will get foo.

Sign up to request clarification or add additional context in comments.

Comments

0

Use a query string.

In a URL a ? indicates the start of the query. It consists of a series of key=value pairs separated by & characters.

The encodeURIComponent function will escape text for inserting as a key or a value.

You can then assign it to the href attribute of the link element or set location.href to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.