0

im having 2 html files, in my first file i have declared a variable and i want to use the same variable in my second file...

my first file code is

<script type="text/javascript">
function topics(clicked_id)
{
    var ids = clicked_id;
    var myObject, fol;
    myObject = new ActiveXObject("Scripting.FileSystemObject");
    if(!myObject.FolderExists("D:/JavaScript/Work/Days/"+ids))
    {
        fol = myObject.CreateFolder("D:/JavaScript/Work/Days/"+ids);
    }
    load_page();
}

function load_page()
{
     open("file:///D:/JavaScript/Work/Topics_Page.html");
}
</script>

i want to use "ids" variable in my second file... Thanks;

alexander.polomodov
  • 4,849
  • 14
  • 35
  • 42
Deepu
  • 1
  • Are you trying to pass `ids` to an entirely different HTML `document`? – guest271314 Sep 24 '17 at 18:06
  • yeah, i wnat to use the ids in other html document... – Deepu Sep 25 '17 at 07:00
  • There are several approaches which can be used to communicate between browsing contexts, several of which are described at Answers at linked Question at Answer – guest271314 Sep 25 '17 at 07:02
  • i didn't find any working procedure for me... help me out... – Deepu Sep 25 '17 at 08:52
  • _"i didn't find any working procedure for me"_ Each of the procedures at Answer provide an approach to pass variables between different browsing contexts. Have you tried the approaches at each of the links at Answer? – guest271314 Sep 26 '17 at 00:59

1 Answers1

0

If the HTML documents have the same origin you can use postMessage, MessageChannel, SharedWorker or storage event to communicate between different browsing contexts, see

You can use localStorage and storage event to use the same object variable, or define a local variable set to the value of localStorage at a different HTML documenta having the same domain.

<!DOCTYPE html>
<html>
<head>
  <title>index</title>
</head>

<body>
  <a href="otherPage.html" target="_blank">otherPage.html</a>
  <h1>set id</h1>
  <script>
    let id;
    let h1 = document.querySelector("h1");
    h1.onclick = e => {
      id = `${Math.E * Math.PI * Math.random()}`;
      localStorage.setItem("id", id);
      console.log(`id: ${id} at ${e.type}`);
    }
  </script>
</body>

</html>

<!DOCTYPE html>
<html>
<head>
  <title>other page</title>
  <script>
    let id = localStorage.getItem("id");
    console.log(`id: ${id} at ${document.title}`);
    onstorage = e => {
      console.log(`id: ${localStorage.getItem("id")} at ${e.type}`);
      id = localStorage.getItem("id");
      console.log(id);
    }
  </script>
</head>

<body>
  <h1>otherPage.html</h1>
</body>

</html>

plnkr https://plnkr.co/edit/m4RIdwgIl74Dk6YmGAgI?p=preview

guest271314
  • 1
  • 10
  • 82
  • 156
  • my posted code is working absolutely perfect, my only point is to use the "ids" variable along with its value in the other html file... Thanks... – Deepu Sep 25 '17 at 05:43
  • @Deepu _"it is just showing the "other page", specified in the otherpage document"_ Not sure what you mean? – guest271314 Sep 26 '17 at 14:28
  • in my first document code i have "ids" variable, i want to get the value of "ids" in another html document... – Deepu Sep 27 '17 at 06:53