0

Let's say I have my website open in two tabs, and i want to get the element with the id "id", can I somehow get the element in the currently focused browser tab?

document.getElementById("id"); 

seems to get the element that was loaded most recently.

Bogdan
  • 1,869
  • 6
  • 24
  • 53

1 Answers1

1

No, since that would be a gaping security hole.

There's a similar question on stackoverflow: Accessing the content of other tabs in browser.

This answer to the linked question provides code to do it, but I had problems in the past with IE, when trying to specify event handlers on the other document (worked fine in Firefox though).

Community
  • 1
  • 1
knittl
  • 246,190
  • 53
  • 318
  • 364
  • then why does it work that way? I mean, let's say i'm in tab nr1 and the document has an input element with id='id'. Now, I open tab nr2, switch to it and set the value of the element with the id='id' to 'xyz'. If i switch over to tab nr1, and try to print the value of the input, it says 'xyz' instead of '' or whatever it was beforehand. – Bogdan Feb 23 '12 at 08:17
  • As for the answer you linked, i figured that would work when I open the new tab from javascript, but in the case i'm trying to solve, the user forces the new tab from within the browser – Bogdan Feb 23 '12 at 08:20
  • Accessing a user created tab through Javascript should not and is not possible. If it actually is, file a bug report with your browser vendor ;) – knittl Feb 23 '12 at 08:28
  • it isn't, as it turns out. Just a case of "been working on so many files at the same time i forgot to upload some" and the event that was supposed to set the value wasn't triggering. – Bogdan Feb 23 '12 at 08:35