简体   繁体   中英

Read iframe redirect (same domain)

I'm working in writing a chrome extension, and as a result I have the peculiar situation of using non-cross domain Iframes, without the ability to alter the page being displayed in the frame.

When a user clicks a certain link, in he iframe, I want to run some JavaScript. The default behavior for clicking that link is to load page targetpage.com. I don't think it's possible, or easy, to read listen for a particular element being clicked inside an iframe

As a workaround, I figure I can check if the iframe reloads, pointing to targetpage.com, and then perform the action.

Note: the action is entirely in the parent page, let's imagine I'm just trying to trigger an alert box.

I know how to trigger JavaScript when an iframe loads. My three questions are:

1) how can I check the url of an iframe?

2) is there a way to check the iframe, url prior to targetpage.com being loaded. Esther than after?

3) is there a better solution?

您可以在后台页面中收听webNavigation.onBeforeNavigate ,它将在导航即将发生时触发,并且您可以在callback参数中获取urlframeId

This may not be the best answer because I haven't played around with this much.

Chrome has a webNavigation API that looks to be something which may come in handy for your extension.

However if you want to get the current domain you're on you'd use...

document.domain

If you're in a subdirectory of that domain you can grab that with...

window.location

It also works with an added hash to the url.

If you want the url without the hash you could use document.referrer or if you feel hacky you could do something like...

var str = window.location
var res = str.toString().split(str.hash)
document.body.innerHTML = res

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