javascript get iframe url's current page on subdomain

我只是一个虾纸丫 提交于 2019-12-31 05:38:28

问题


I need to get the page url of what the iframe on my pages are on, as we use the iframe to deal with ordering products. However, for the life of me I can't seem to get it to display the iframe's URL (display is a test, as it will auto-email when I see it works)

The code:

<button onClick="myFunction()">Test</button>
<script>
function myFunction() {
alert(document.getElementById("iFrame").documentWindow.location.href);
}
</script> 
<iframe src="https://iframe.mydomain.co.uk" id="iFrame" width="98%" height="100%"></iframe>

This code is being used on https://products.mydomain.co.uk. The alert, as I said, is only there so I can just press the button to see it showing the new domain, so that I can do what is needed for the rest of it.

Any ideas?


回答1:


add document.domain = 'your.domain' in both the pages.

like this. don't forget both parent.top

document.domain = 'corp.local';

only parent like

document.domain = 'corp';

won't work.



来源:https://stackoverflow.com/questions/32501901/javascript-get-iframe-urls-current-page-on-subdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!