0

I'm just trying to retrieve the dom of the two html page on the same javascript file.

When i try to retrive the the dom of an element of the first html page, it works, but the elements of the second page return me undefined. How can i try to retrieve the element of the second page ?

firstpage.html

<p id="first">test first</p>

secondpage.html

<p id="second">test second</p>

file.js

var first = document.getElementById("first").innerHTML;
console.log(first); // will show me "test first"
var second = document.getElementById("second").innerHTML;
console.log(second); // will show me "undefined" .. :(

Thanks for the help !

Zahreddine Laidi
  • 560
  • 1
  • 7
  • 20
  • 1
    can you be a bit discrete about the question, maybe using a code snippet or something? – Animesh Saraswat Oct 04 '18 at 09:38
  • Sure, will edit it right now ;) – Zahreddine Laidi Oct 04 '18 at 09:40
  • 1
    You're trying to approach a solution in wrong direction. Things doesn't have to be this complicated. I don't know what exactly you're trying to achieve. Maybe you can take a look at SPA? – WarPro Oct 04 '18 at 10:19
  • Check your `console` for any possible error. – Zahid Zuhair Oct 04 '18 at 10:32
  • got the problem, JAVASCRIPT is an interpreted language, hence in the first case it gets the id 'first' and console's the result, but look carefully it also gets the error for the second one saying **Cannot read property 'innerHTML' of null**. That is because in the first page it doesn't get's the `id = 'second'`. – Animesh Saraswat Oct 04 '18 at 10:37
  • Now since JS is interpreted so it stops it's execution as soon as it gets an error, hence when your second page triggers the javascript file it doesn't get the `id = 'first'` and there it stops it's execution, resulting into error instead of the desired result. – Animesh Saraswat Oct 04 '18 at 10:40
  • you can try doing this. `if(document.getElementById("first")){ var first = document.getElementById("first").innerHTML; console.log(first); } else { var second = document.getElementById("second").innerHTML; console.log(second); } ` – Animesh Saraswat Oct 04 '18 at 10:51
  • The problem is that the console.log will never disp the second if we have the "first" element on the first html page... – Zahreddine Laidi Oct 04 '18 at 11:44
  • Try [this](https://stackoverflow.com/a/42526074/5561276) solution. – Zahid Zuhair Oct 04 '18 at 12:02
  • I think that you didn't understand what i'm trying to do, i have an array on the first.js file that i feed with some information with my first page, when i finished to feed my array with my first page, i want to re-use this same array with his information in it, on the second html page. So how can i do this guys ?? @WarPro – Zahreddine Laidi Oct 04 '18 at 12:09
  • I think _Question_ is different than above _Quoted_ text!! – Zahid Zuhair Oct 04 '18 at 13:40
  • true, see my true question here : https://stackoverflow.com/questions/52646813/retrieve-content-of-array-used-on-a-different-js-file-javascript/52647645#52647645 @ZahidZuhair – Zahreddine Laidi Oct 04 '18 at 13:42

0 Answers0