/    Sign up×
Community /Pin to ProfileBookmark

Send variable to a new window

If i click on a button in order to load another page and want the navigation link to be selected in the new window. How do I do?

Is there something like “super global variable”?

The global variable is only for the current window.

https://jsfiddle.net/zgvn3upe/28/

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumFeb 15.2020 — Is there something like "super global variable"?[/quote]No, unfortunately there is not.

If you open the second page in a new window by use of window.open you can access variables or call functions in the adjacent page in both directions. However I do not recommend this as the behaviour is not reliably predictable. Recent browsers open the second page in a new tab but there is no guaranty.

For handing over variables from one page to another you can use a query string like this:

`<a href="new-page.html?var1=value1&var2=value2">Link</a>`

You can then access these variables easily by use of URLSearchParams:

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
Copy linkTweet thisAlerts:
@sibertauthorFeb 18.2020 — > @Sempervivum#1614707 No, unfortunately there is not.

Fortunately there is a way, but not as a variable but a "localStorage":

``<i>
</i>const nav = document.querySelector('.nav');
const subnav = document.querySelector('.subnav');

nav.addEventListener("click", opensub);

function opensub() {
var w = window.innerWidth;
if (w&lt;700)
window.localStorage.setItem('subnav', "open");
}

window.onload = function(){
var open = localStorage.getItem("subnav");;
subnav.classList.add(open)
window.localStorage.setItem('subnav', "");
}<i>
</i>
``
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...