Tuesday, May 28, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  32] [ 7]  / answers: 1 / hits: 49979  / 11 Years ago, wed, october 16, 2013, 12:00:00

Here is my code on http://my-localhost.com/iframe-test.html



<html>
<head><title>Welcome Iframe Test</title></head>
<body>
<iframe src=http://www.my-website.com/index.html width=500 height=500></iframe>
<script type=text/javascript>
function alertMyMessage(msg){
alert(msg);
}
</script>
</body>
</html>


Here is code on http://www.my-website.com/index.html



<html>
<head></title>Welcome to my Server</title></head>
<body>
<h1>Welcome to My Server</ht>
<a href=javascript:void(0) title=Click here onClick=parent.alertMyMessage('Thanks for Helping me')>Click Here</a>
</body>
</html>


When i Click the Click Here Link. i got following Error.




Uncaught SecurityError: Blocked a frame with origin
http://www.my-website.com from accessing a frame with origin
http://my-localhost.com. Protocols, domains, and ports must match.




Please Help me to Fix this Issue, or give some other solution for this.


More From » html

 Answers
17

You cannot access the DOM of a page loaded in a frame in a different origin. This is blocked for security reasons (imagine a random website you visited opening your web mail service in a hidden iframe and you can see why).



The closest you can come, and then only if you control both websites, is to pass messages between them using the web messaging api.



In one page, write a function to handle the messages and then add it as a message event listener.



function receiveMessage(event)
{
alert(event.data);
}

addEventListener(message, receiveMessage, false);


In the other, send the message:



parent.postMessage(This is a message, *);


See MDN for more information


[#74946] Tuesday, October 15, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harveys

Total Points: 113
Total Questions: 88
Total Answers: 79

Location: Oman
Member since Fri, Dec 23, 2022
1 Year ago
;