2

I need to open a new window and return an element contained in it.

Say we have page A and page B, I want:

  • open B from A
  • get the element interested in B
  • return that element to A
  • I tried to do so in this manner, but it doesn't work:

    var newwindow = window.open("http://www.example.com"); var elem = newwindow.document.getElementById('my-id').value; 

    Where am I wrong? Has anyone some advice to me?

    6
    • wat have you tried till now ?? Commented Feb 15, 2012 at 10:38
    • 2
      @VivekChandra — There is code in the question. Commented Feb 15, 2012 at 10:39
    • 2
      What doesn't work about it? Is there a Javascript error? Does it execute but return nothing? Does it execute but return the wrong value? Commented Feb 15, 2012 at 10:39
    • Sorry, I forgot a point. If I try to print somehow (ex. alert) this value via script I get nothing, I mean, neither the alert box. Commented Feb 15, 2012 at 10:49
    • 1
      @MarcoSecchiero — That just means the script terminates with an error before it reaches the alert statement. You've been asked to report those errors twice already. Commented Feb 15, 2012 at 11:21

    2 Answers 2

    5

    Since you are using an absolute URI, I'm going to assume that you are trying to grab data from a different website. You'll therefore be blocked by the same origin policy.

    If that isn't the case, then you're probably hitting a race condition by trying to read the content of the document before it has finished loading.

    It would be a lot easier to help if you provided the error messages that your browser is almost certainly logging to its JS console.

    Sign up to request clarification or add additional context in comments.

    1 Comment

    This is the error I got: TypeError: Cannot call method 'getElementById' of undefined
    0

    If the new window has the same protocol, domain and port, your code should work. If it's on another domain, you can't do this for security reasons.

    If you control both pages, you could use window.postMessage.

    Comments

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.