passing in javascript values into iframe tag

青春壹個敷衍的年華 提交于 2019-12-03 07:28:35

The only way you can pass values directly to an iframe of a page with a different domain is through the url, as querystring values, anchors, or perhaps some form of rewriting. Even if it's on the same domain, that's probably the easiest and safest way to pass it.

Main document:

document.getElementById('IframeID').src = "somepage.html?seed=" + custom_seed;

Inner document:

var seed = window.location.search.substring(window.location.search.indexOf('seed=') + 5);
if (seed.indexOf('&') >= 0) {
    seed = seed.substring(0, seed.indexOf('&'));
}
Squilo

A simple away to get the value in iframe file :

  document.getElementById("youfiled").value="";

And use the echo php $_GET['seed'];

inside of the coma element;

Here's a neat workaround with no ugly url variables. While you can't send variables into an iframe, you can call a function to run.

Begin by programming a function in the destination page to receive and set the variable:

<script type="application/javascript">
    function variable(value) {
        var seed = value;
</script>

Then all you need to do is call the function from the origin page:

<script type="application/javascript">
    document.getElementById("IframeID").contentWindow.variable("value");
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!