简体   繁体   English

使用JavaScript打开另一个HTML页面时传递变量

[英]Passing variable when opening another html page using JavaScript

This is probably a really silly question, but I can't find it online anywhere and I've been looking for at least an hour. 这可能是一个非常愚蠢的问题,但是我无法在任何地方在线找到它,而且我一直在寻找至少一个小时。

I have a link <a href="MusicMe.html" id="instrumentsNav">Instruments</a> which I want to get the ID of it once clicked, as I need to pass some variable to the page I am opening to know that the instruments link was clicked. 我有一个链接<a href="MusicMe.html" id="instrumentsNav">Instruments</a> ,我想在单击该链接后获取其ID,因为我需要将一些变量传递给我要打开的页面知道仪器链接被单击。 This is being called from productInformation.html 这是从productInformation.html调用的

I have also tried doing <a href="#" onclick="instrumentsClick()" id="instrumentsNav">Instruments</a> and then in my JavaScript, window.open("MusicMe.html", "_self"); 我还尝试过<a href="#" onclick="instrumentsClick()" id="instrumentsNav">Instruments</a> ,然后在我的JavaScript中, window.open("MusicMe.html", "_self"); and then tried passing a variable that way, but still absolutely no luck. 然后尝试以这种方式传递变量,但绝对没有运气。 Any help as to how I would pass a variable back to the page when it opens would be brilliant. 关于如何在打开变量时将其传递回页面的任何帮助都很棒。

Once it opens, I am using the variable to set the ID of an element so it only displays a certain set of the information, which is working on it's own, but when I go back to try and call it, it's always thinking it is showing them all as I cannot work out how to set the variable to define it. 一旦它打开,我就使用变量来设置元素的ID,因此它仅显示一组特定的信息,这些信息可以自己处理,但是当我回去尝试调用它时,它总是认为它是显示所有这些,因为我无法解决如何设置变量以对其进行定义。 Unfortunately I need to use JavaScript not PHP. 不幸的是,我需要使用JavaScript而不是PHP。

Thanks. 谢谢。

I would just tell you some ways, hope you would be able to implement it: 我只想告诉您一些方法,希望您能够实现它:

  1. Use query params to pass the variable. 使用查询参数来传递变量。

When you're navigating to another page, add query params to the url. 导航到另一个页面时,将查询参数添加到URL。

window.open("MusicMe.html?variable=value", "_self");

In your next page, check for queryParam by getting window.location.href and using regex to split the params after ? 在您的下一页中,通过获取window.location.href并使用regex在?之后拆分参数来检查queryParam ? and get that data. 并获取数据。

  1. Use localStorage/cookies 使用localStorage / cookie

Before navigating to the next page, set a variable in your localStorage 导航到下一页之前,请在localStorage中设置一个变量

localStorage.setItem("variable","value");
window.open("MusicMe.html", "_self");

In your second page, in the window load event. 在第二页的窗口加载事件中。

 $(function() {
      if(localStorage.getItem("variable")) {

       // set the ID here
       // after setting remember to remove it, if it's not required
       localStorage.removeItem("variable");
      }

  });

You can use localStorage to keep the value, or use parameter like 您可以使用localStorage保留值,或使用类似

href="MusicMe.html?id=111"

to pass the value to new page. 将值传递到新页面。

You can always pass a GET variable in you re URL just like this myhost.com?var=value You can get the value of this variable by parsing the URL using Js see this 您总是可以像这样myhost.com?var=value一样在URL中传递GET变量。您可以通过使用Js解析URL来获取此变量的值。

How can I get query string values in JavaScript? 如何获取JavaScript中的查询字符串值?

You can simply pass # value into url, get it and then match it with ' rel ' attribute given on specified element. 您可以简单地将#值传递到url中,获取它,然后将其与指定元素上提供的' rel '属性进行匹配。 Here I have done materialize collapsible open from link on another page. 在这里,我完成了另一页上的可折叠打开。

JS: JS:

var locationHash = window.location.hash;
var hashSplit = locationHash.split('#');
var currentTab = hashSplit[1];
$('.collapsible > li').each(function() {
   var allRels = $(this).attr('rel');
   if(currentTab == allRels){
       $(this).find('.collapsible-header').addClass('active');  
       $(this).attr('id',currentTab); 
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM