使用 jQuery 重新加载页面后隐藏 iframe ID



我有一个关于在使用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()"。让我知道这是否适合您

最新更新