views:

28

answers:

3

can we pass any info., say a string between html pages using javascript. No forms, input fields I am using just want to pas some info in hyperlink.

A: 

By the expression "info in hyperlink" i guess you want to let user inititate communication by clicking on it. This could work:

<a href="" id="mylink">Click here</a>

<script>
  extra_data = "key1=value1&key2=value2";
  document.getElementById("mylink").href = "http://example.com/?" + extra_data;
<script>
LatinSuD
A: 

Yes. There are many simple ways to do this if all you need to do is pass a short string. You could include a string in a link after a '#' and have that read by Javascript on the next page (looking at location.hash).

Without knowing more, its hard to narrow the approach down to suit your needs.

Simple Example:

one.html

...
<a href="two.html#this_is_data_could_also_be_encoded_json_string_too">Pass data</a>
...

two.html

<script>
var passedData = location.hash.replace('#','');
alert('You passed this data: ' + passedData);
</script>
michael
thanks, but can I have a small example...
Rahul Utb
Added a very simple example demo'ing this between two html files.
michael
A: 

What's your use-case?

There are lotta ways to do it. Say...

  1. Storing the data in a session cookie (Assuming that you're accessing 2 pages from the same domain.)
  2. Passing data in query string?

For example,

<input id="btnClickMe" onclick="btnClickMe_onClick()" type="button" />

...
<script type="text/javascript">
function btnClickMe_onClick() {
  var data = "foo";
  var urlOfNewPage = "http://www.test.com/testPage?data=" + data;
  // Redirect user to the new URL
}
</script>
DashK