简体   繁体   中英

Selecting content inside of iframe

The design for the project I'm working on has live chat plugin and an image on the header that says online/offline. I'm trying to work on script that will place the "online" image when the agent is online.

The heading of the plugin displays the status, like so

<span __jx__id="___1_livehelpbutton__agentstatus" class="agentStatus" style>Offline</span>

I would like to get the innerHTML of the tag and use it in the conditional of an if statement, which, if the status is "online" will display the "online" image and otherwise display the offline image. That is where I run into an issue. I'm having trouble selecting for this tag. When I try this on the console, I get [] and no change on the screen.

$('.agentStatus').css('background','red');

When, as a test on the CSS page, I try .agentStatus { background: red; }, the background turns red.

The iframe it is in is does not have a different url:

<iframe __jx__id="___$13__iframe src="javascript:void(document.write('<!DOCTYPE HTML PUBLIC -//W3C//DTD HTM…order-box}</style></head><body></body></html>'), document.close())" frameborder="0" style=" background-color: transparent; vertical-align: text-bottom; overflow: hidden; position: relative; width: 100%; height: 100%; margin: 0px; z-index: 999999;">
    #document
        <!DOCTYPE html PUBLIC"-W3C//DTD HTML 4.0 Transitional/EN" "http:www.w3.org/TR/html4/loose.dtd"
            <html style="overflow-y: hidden">
                 ...

The problem is that you simply cannot access the content in an iframe without first grabbing the document that is being displayed by the iframe .

This post explains this better than I can.

There is no actual way to do this in JavaScript or jQuery, unless the iframe 's source is on the same domain (research 'Cross-Domain and JavaScript'). If they are on the same domain, however, you should be able to select (and manipulate) elements using the following:

$("#iframeID").contents().find(".agentStatus").css("background", "red");

This post shows a few alternative solutions to this, as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM