onmousemove event does not fire from within external source?
Asked Answered
B

2

7

I am loading up an external webpage (i.e. www.duckduckgo.com in this example) within a div on my webpage. I want to get my mouse X and Y position while within and outside the div, but when I am inside the div, it seems that the webpage blocks the onmousemove event from firing. However, the onmouseover event fires only once when entering the div.

Here is example code that illustrates my problem:

function mouseEvent(event) {
      var x = event.clientX;
      var y = event.clientY;

      document.getElementById('label').innerHTML = 'X=' + x + ' Y=' + y;
}
html {
    height: 100%;
    width: 100%;
}
body {
    height: 100%;
    width: 100%;        
    overflow: hidden;
    margin-left: 0px;
    margin-top: 0px;
    margin-bottom: 0px;
    margin-right: 0px;
}
#form1 {
    height: 100%;
    width: 100%;
}
#pageDiv {
    height: 100%;
    width: 100%;
}
#page {
    height: 100%;
    width: 100%; 
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div id="pageDiv"> 
            <label id="label">hello</label>
            <object id="page" type="text/html" data="https://duckduckgo.com/" onmousemove="mouseEvent(event)" onmouseover="mouseEvent(event)">
            </object>
        </div>
    </form>
</body>
</html>

How can I get the mouse X and Y position anywhere on this webpage (i.e. not just on top of the div holding the external source)? I tried adding event.preventDefault(); to the beginning of the mouseEvent function, but that did nothing in the realms of helping.

I am guessing the external webpage is stealing my focus away. Is this the case? Is there anyway I can achieve constant X and Y coordinate updating?

Boylston answered 21/7, 2017 at 18:56 Comment(5)
Attach the event to window instead. It could be the same-origin policy is preventing events from a tainted object.Montague
@Montague That is not work. I added the following code $(window).mousemove(mouseMove(event)); to the script section of my html document. The problem is still present.Boylston
You can have a look at this answer. As I understand it, you may be in the "out of luck" situation.Dang
Here is another answer to a similar question: https://mcmap.net/q/143499/-detect-click-into-iframe-using-javascript.Dang
@ConnorsFan yeah, I am starting to get the idea that this just isn't possible. I know if I was doing this in C++ and/or any non-web language I could do exactly what I would want, but I guess you can't in the browser. I thought I could possibly intercept the mouse event and relay them to two different divs, but the solutions out there have been non-working. I will most likely post an answer soon detailing everything, but for the mean time, I will wait to see if anyone has any ingenious cracksBoylston
P
3

function mouseEvent(event) {
      var x = event.clientX;
      var y = event.clientY;

      document.getElementById('label').innerHTML = 'X=' + x + ' Y=' + y;
}

function removeCheat() {
     // remove cheat div or remove right/bottom position. 
     // Not sure what your ultimate goal is.
}
html {
    height: 100%;
    width: 100%;
}
body {
    height: 100%;
    width: 100%;        
    overflow: hidden;
    margin-left: 0px;
    margin-top: 0px;
    margin-bottom: 0px;
    margin-right: 0px;
}
#form1 {
    height: 100%;
    width: 100%;
}
#pageDiv {
    height: 100%;
    width: 100%;
}
#page {
    height: 100%;
    width: 100%; 
}
#cheat {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <div id="cheat" onmousemove="mouseEvent(event)" onmouseover="mouseEvent(event)" onmousedown="removeCheat()"></div>
    <form id="form1" runat="server">
        <div id="pageDiv"> 
            <label id="label">hello</label>
            <object id="page" type="text/html" data="https://duckduckgo.com/">
            </object>
        </div>
    </form>
</body>
</html>
Paco answered 21/7, 2017 at 21:5 Comment(1)
This is great, except my mouse clicks are not going threw to the object (i.e. iframe) anymore. Take a look, you can't click on the "duckduckgo" website anymore; you can't interact with the website anymore. I will play around with it, but if you are able to do this as well, I will accept your answer as the solution.Boylston
S
1

WHY IT DOESN'T WORK ###

Let me get this straight for you ...

since iframes or objects are designed to be a simple wormhole from your website to the requested source, they have their own functionality and objects !

there are lots of tricks you can play to determine the axis of the cursor without the possibility to interact with the wormhole , but if you do want to interact with it there are other ways ...

HOW TO GET IT TO WORK ###

one of them is the good old ajax , using it you can load the entire website on a div and do even more than you can on wormholes ! since with ajax you break down the web page to it's skeletons , you'll not only be able to get your X and Y you can even personalize you page =)

since it's ajax you can go crazy on it and build your own search field and only get the results and links from the desired search engine ( since that's you project ) OR you could simply load the entire page on a div or any other element.

if you and XML don't get along well like i don't , JQUERY could lend a hand

Soulful answered 28/7, 2017 at 19:39 Comment(6)
Can you please provide a modification to the code above? Like I understand what you are saying, but my attempts have been unsuccessful. I really am starting to think this isn't possible, but if you have a solution for my tiny example above, please post an edit to this post. If you provide a working example, I will give you the bountyBoylston
Checkout W3Schools.com , it's search field sends data to google and then receives the results and categorize them ! but since Ajax is designed for internal sources in order to get it to work with external sources The Website you intend to show Must enable a feature which you can send and receive data from which most search engines do ...Soulful
And consider this : if you find a way to use ajax with a website since it's out of this methods reach there would be lots of tricks involved to get it to work ! therefore i recommend you frame your ultimate intentions of getting the cursor axis for us may be able to help youSoulful
so, technically your answer isn't really an answer to my question. W3Schools.com search field is accessing one of Google's APIs that interfaces with it. It is getting search results back, not the actual html page with all the graphics. This isn't possible, I am beginning to see. The only way I could possibly see it working is to manually configure all the POST/GET requests, but even doing that might not necessarily work. If you aren't sure how to get a working attachment to my example, then I'll post soon explaining why it can NOT work.Boylston
i literally mentioned it is not doable the way you wanted using iframes or objects ! but since i'm a "Nothing Is Impossible" guy i suggested using Ajax which is totally doable BUT HARD ... so if i were you and i wanted to do it i'd do it ! but consider this as well : you have to do everything yourself and only leaving the request parts to Ajax ! so do a total evaluation on whether it's worth it or not , my answer was trying to tell you it's not doable your way but there is another way ! and it's unbelievably HARD ...! Good Luck budSoulful
BTW , the reason i'm not offering you a working attachment is i've tried this before ... and it's not a plugin kind of method ! it's different depending on your projectSoulful

© 2022 - 2024 — McMap. All rights reserved.