I am creating a form with Javascript only. I am trying to use Javascript to get the value of the input field which is inside an iframe
. Is it possible to get the value of a field that is inside an iframe
?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
Yes it should be possible, even if the site is from another domain.
For example, in an HTML page on my site I have an iFrame whose contents are sourced from another website. The iFrame content is a single select field.
I need to be able to read the selected value on my site. In other words, I need to use the select list from another domain inside my own application. I do not have control over any server settings.
Initially therefore we might be tempted to do something like this (simplified):
HTML in my site:
HTML contents of iFrame (loaded from
select.php
on another domain):jQuery:
However, I receive this javascript error when I attempt to read the value:
Blocked a frame with origin "http://www.myownsite.com" from accessing a frame with origin "http://www.othersite.com". Protocols, domains, and ports must match.
To get around this problem, it seems that you can indirectly source the iFrame from a script in your own site, and have that script read the contents from the other site using a method like
file_get_contents()
orcurl
etc.So, create a script (for example:
select_local.php
in the current directory) on your own site with contents similar to this:PHP content of select_local.php:
Also modify the HTML to call this local (instead of the remote) script:
Now your browser should think that it is loading the iFrame content from the same domain.
You can Get value by JQuery
work on only same domain link