Reputation: 11205
I am trying to use jQuery/javascript with an iframe containing google form. Here is the code snippet:
<body>
<iframe id="myFormFrame" src="https://docs.google.com/forms/d/smfjkafj809890dfafhfdfd/viewform?embedded=true" width="760" height="1750" frameborder="0" marginheight="0" marginwidth="0">Loading...</iframe>
<button id="mybtn">Click</button>
<script type="text/javascript">
$('#mybtn').click(function(){
var m=document.getElementById("ss-form");
alert(m.innerHTML);
});
</script>
The button on click should give the innerHTML of the form(which on loading inside iFrame has an id of "ss-form"). But its not working. So what could be the reason(cross domain iframe?)? And what is the way to make jQuery work with google forms?
If it helps, I am trying to put custom ajax validation in place(for example, the email id field must contain emails from a specific list obtained from different database).
EDIT
Well it looks like a cross domain issue. I had this doubt in mind. But after looking at this article, it looks like possible.
Upvotes: 0
Views: 2210
Reputation: 943759
document.getElementById
searches the current document, not other documents loaded into frames.
If the framed document was on your own site then you could access the frame and then access the document (via contentDocument
) first. Then call getElementById
on that.
Since it is cross-origin, that will fail. The reasons for this are obvious if you consider what would happen if Mallory's evil website were to stick your bank's online banking site into a frame while you were logged in.
If you had the cooperation of Google (you don't) then you could pass messages between the frames using postmessage
.
Well it looks like a cross domain issue. I had this doubt in mind. But after looking at this article, it looks like possible.
They aren't using a form hosted by Google. They've used Google tools to design the form and then copied the HTML to their own site. If Google ever implement CSRF protection, that will break.
Upvotes: 2