Reputation: 39
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
Upvotes: 2
Views: 128
Reputation: 1104
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.
Upvotes: 2
Reputation: 16733
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);
Upvotes: 1