javascript - 错误: Permission denied to access property '$'

标签 javascript jquery twitter-bootstrap iframe

大家。 我有以下情况:

我有: http://example.com/http://example.com/new

在 example.com 中,我使用 fancybox iframe 在 example.com/new 域中加载了一些表单。

我的表单基本上显示了一些供用户输入其 pessoal 数据的字段,例如姓名、电话等...提交后,我显示了一些来自数据库的用户协议(protocol)条款和一个供用户输入的复选框说他同意这些条款。

在他检查并提交后,我想提醒一些成功消息,并关闭 fancybox 模式/iframe,仅此而已。

在表单页面中,我已经加载了jquery和bootstrap。因此,当用户同意时,我打印:

<?php
     echo "
          <script>
          alert('Some success message!');

          $(document).ready(function(){
              parent.$.fancybox.close();
          });
         </script>
     ";
?>

我有三种形式,其中一种有效,另两种形式我得到:

Error: Permission denied to access property '$'

有效的表单与其他两个表单之间的唯一区别是,在有效的表单中,我没有来自数据库的协议(protocol)条款,只有复选框。

我可以把我的整个代码放在这里,但这将是一个巨大的问题。但如果你们需要的话,我可以更新。

抱歉我的英语不好,如果我不清楚,请原谅我。

更新:

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <?php
        /* Connect with DB */
        require_once('require/conectar.php');

        if(!empty($_POST))
            foreach($_POST as $k => $v)
                $$k = $v;
    ?>

    <script type="text/javascript" src="http://example.com/new/assets/js/jquery.js"></script>
</head>
<body>
<?php if(!isset($agree) and !isset($next)):     ?>
    <h1>The form</h1>
    <form method="post" action="">
        <label>Your name:</label>
        <input type="text" name="name">
        <br>
        <label>Your email:</label>
        <input type="text" name="email">
        <br>
        <input type="submit" name="next">
    </form>
    <?php
        else:
            $error = (!isset($name)) ? true : false;
            $error = (!isset($name)) ? true : false;

            if($error)
            {
                echo '<script>You must fill all fields before submit.</script>';
                exit;
            }

            $qrr = mysql_query("SELECT * FROM `terms`");
            $terms = mysql_fetch_object($qrr);
    ?>
        <h1>Terms:</h1>
        <?php echo $terms->content; ?>
        <form method="post" action="">
            <input type="hidden" value="<?php echo $name; ?>" name="name">
            <input type="hidden" value="<?php echo $email; ?>" name="email">
            <input type="checkbox" value="1" name="accept"> I agree.
            <input type="submit" name="agree">
        </form>
    <?php
        endif;
        if(isset($agree))
        {
            /*
                Here i mail me the user data.
            */

            echo "
                <script>
                alert('Soliciação Realizada com sucesso!');
                $(document).ready(function(){
                    parent.$.fancybox.close();
                });
                </script>
            ";
        }else
        {
            echo "<script>alert('You need to agree with the terms to proceed.');</script>";
        }
    ?>
    </body>
</html>

最佳答案

这是一个浏览器安全问题。虽然有几种解决方法,但最好的方法可能是使用 postMessage API .

在您的 example.com 父页面上,添加如下代码:

function handleMessageFromiFrame(event) {
  alert('Some success message: ' + event.data);
  //$.fancybox.close();
}

window.addEventListener("message", handleMessageFromiFrame, false);

然后在您的子 example.com/new iframe 上添加如下代码:

var parentOrigin = "*"; // set to http://example.com/ or whatever for added security.

function sendMessageToParent(){
  parent.postMessage("button clicked", parentOrigin);
}

$('#submit-btn').click(sendMessageToParent);

这是一个实际的例子:

当您单击子页面中的按钮时,它会使用 postMessage 通知父页面。然后父级监听消息并执行您想要的任何操作。

关于javascript - 错误: Permission denied to access property '$' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28049188/

相关文章:

javascript - 当我提交表单时,从 jquery datepicker 中的开始日期和天数计算结束日期

javascript - 在一个函数完成后运行函数 - JQuery animate

javascript - 图像轮播 onmouseover data-slide-to

css - Bootstrap - 容器中的中心选项卡

javascript - 如何检查元素是否在屏幕外

javascript - 使用jQuery添加数据后如何执行搜索功能?

javascript - Rails 复选框隐藏字段类

jquery - localScroll 并缓解滚动问题

javascript - 如何缓慢地向 ember 组件传递重复的数据列表

html - 在文本重叠时放大图像(在图像中)