javascript - 使用 jQuery 在页面重新加载后隐藏 iframe id

标签 javascript jquery iframe

我有一个关于使用 jQuery 在页面重新加载后隐藏 iframe id 的查询。 我有一个外部实时聊天(使用以下 iframe )

<iframe id="customer-chat-iframe" name="mcs_1380166852448_" src="https://testlivechat.test.com/php/app.php?widget-iframe-content" marginwidth="0" marginheight="0" frameborder="0" allowfullscreen="" style="background: transparent; border: none; outline: none; position: fixed; display: block; z-index: 999999; bottom: -355px; right: 30px; overflow: hidden; min-width: 279px; min-height: 368px; width: 340px; height: 400px; margin: 0px; padding: 0px;"></iframe>

正在外部网站调用实时聊天。

<script type="text/javascript" src="//testlivechat.test.com/php/app.php?widget-init.js"></script>

我的要求是我需要首先隐藏聊天框(在整个页面重新加载后),然后在单击某些特定事件(例如按钮单击)时,聊天框应该显示..

我用 jquery 做了同样的尝试,但一开始无法隐藏。

无效代码:

(function( $ ) { 
 $('#customer-chat-iframe').hide(); 
})(jQuery);

最佳答案

尝试使用 JS Cookie:

/** This is load on page ready. **/
$( document ).ready(function() {
  var isCookieSet = getCookie("c_chat"); 
  if(isCookieSet == "" || isCookieSet == null){
    $('#customer-chat-iframe').css("display", "none");
  }
  else{
    $('#customer-chat-iframe').css("display", "block");
  }
});


/*Function to get cookie value **/
function getCookie(c_name) {
  if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf(c_name + "=");
    if (c_start != -1) {
        c_start = c_start + c_name.length + 1;
        c_end = document.cookie.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = document.cookie.length;
        }
        return unescape(document.cookie.substring(c_start, c_end));
    }
  }
  return "";
}




/** This will execute on click of radio button and set the cookie **/
function onRadioButtonClicked(){
  var isCookieSet = getCookie("c_chat"); 
  if(isCookieSet == "" || isCookieSet == null){
    document.cookie = "c_chat=1";
  }
  $('#customer-chat-iframe').css("display", "block");
}

您需要在单选按钮上调用“onRadioButtonClicked()”。 让我知道这是否适合你

关于javascript - 使用 jQuery 在页面重新加载后隐藏 iframe id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46648762/

相关文章:

jquery - 如何使用jquery获取类名

jquery - 将 Bootstrap 轮播指示器移至外部并更改其图标

google-chrome - chrome 更新后,SSO 登录在 iframe 中不起作用

javascript - 通过jquery链接两个div - 一个div中发生的任何事情都会发生在另一个div中

javascript - 用户在文本区域中输入文本后显示 Div?

javascript - jquery从完整的html页面中删除最后一个表的方法

javascript - 修改 javascript 代码,使其在 iframe 中获取当前选定的文本

javascript - 无法使用 angularJS 将 URL 加载到 iframe

javascript - Parse.com后台作业云代码无法调用未定义的方法 'then'

javascript - 使用 CORS 请求设置 Cookie