2

I have one page with a variable 'g'. On click I need to move the current definition of 'g' to external HTML page with a table. Those 2 pages are connected between each other with JS file. I need to do it only using js / jquery.

Here is my weird code that occurred when I tried to do it by myself.

function totable() {// function to add variable g to table count
    tableContent = "<tr>"; 
    for( ;g> 1;) {
        tableContent += "<td>" + g + "</td>"; 
    }
};

There must be some problem with connection between those 2 HTML files I suppose What is the best way to do it? Any suggestions? Thanks in advance

Alexandar
  • 916
  • 2
  • 9
  • 22

2 Answers2

2

The only option you have to achieve this is by server communication or use of localstorage as long as the two pages are on the same server.

Using localStorage, the page with totable() function will have to write to it using

localStorage.setItem()

The recieving end will have to read by using

localStorage.getItem()

LocalStorage is a shared storage space available to all pages within the same domain.

Ole Borgersen
  • 1,094
  • 9
  • 9
  • This is the better answer. I just discovered that even IE8 does support local storage, so it is pretty well supported across browsers. – Mister Epic Dec 10 '13 at 16:51
1

Typically you would want to do this with server-side code, but you can pass values with the querystring on the client side fairly easily:

 window.location = "nextpage.html?variable=" + g;

And then to read it, per this previous SO answer at How can I get query string values in JavaScript?:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var value = getParameterByName(variable);
Community
  • 1
  • 1
Mister Epic
  • 16,295
  • 13
  • 76
  • 147