/    Sign up×
Community /Pin to ProfileBookmark

Capturing external events…

OK, hopefully this question isn’t too confusing…

I need to know if this is possible or not. I think not, but wanted to check anyway.

OK, in main browser window, we have what I will call “doc1”. Another page is opened with the Javascript open function, we will call this page “doc2”. What I need is for doc2 to be able to capture an onClick event that is triggered in doc1. I need to be able to capture the element that you click in. Imagine this scenario:

  • 1. You open up a web page that has a form (doc1)

  • 2. You open up doc2 by clicking a button

  • 3. You click in a textarea in doc1, and doc2 is able to see which textearea element you clicked in.
  • I haven’t gotten this to work, however, and I think this might be violating some kind of built in Javascript security. Here is the code I started playing with, but didn’t really get anywhere. Looking for an idea that works, or a confirmation of my suspicion that I can’t do what I am trying to do.

    Note: The two pages might not end up being from the same domain, so take that into consideration since I know Javascript has security checks for some cross-domain actions.

    [code]
    <!– doc1 –>
    <html>
    <head><title>clicktest</title>
    <body>
    <form name=”test” action=””>
    <input type=”text” size=20><br>
    <textarea name=”text” cols=60 rows=5></textarea><br>
    <input type=”button”
    onClick=”javascript:void(open(‘test2.html’,’test2′, ”));”
    value=”Open Window”>
    </form>
    </body>

    </html>
    <!– end doc1 –>

    <!– doc2 –>

    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    </body>
    <script language=”javascript”>
    alert(window.opener.document.title);
    window.opener.document.onClick = doIt;

    function doIt(evt) {
    alert (“got here”);
    if (evt.srcElement.type == “textarea” ||
    evt.srcElement.type == “text”) {

    alert (evt.srcElement.type);
    evt.srcElement.value = “hello world?”;

    }
    }
    </script>

    </html>
    <!– end doc2 –>
    [/code]

    to post a comment
    JavaScript

    1 Comments(s)

    Copy linkTweet thisAlerts:
    @gil_davisMay 02.2003 — The two pages might not end up being from the same domain[/quote]I didn't bother reading any further. I can confirm for you that Javascript is not allowed to cross domains for security reasons, and there is no work-around other than placing both documents in the same domain.
    ×

    Success!

    Help @cmelnick spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.7,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...