针对特定页面排除jQuery的document.ready事件

4
我有以下代码:

我有以下代码:

$(document).ready(function() {
   $(".rightImage").last().append("<iframe src='http://images.findel-     education.co.uk/EmailSignup/website_email_sign_up.html' width='180px'frameborder='0' height='250px'></iframe>");  
});

上面的代码向一个类添加了一个iframe,问题是它出现在每个页面上。有没有一种方法来过滤掉上述函数,使其在我的主页以外的每个页面上都能工作? 我无法控制父级的html,所以我必须这样做。
谢谢。
6个回答

2

好的,试试这个:

$(document).ready(function() {
    if (document.location.href != 'homepage_url') {
        $(".rightImage").last().append("<iframe src='http://images.findel-     education.co.uk/EmailSignup/website_email_sign_up.html' width='180px'frameborder='0' height='250px'></iframe>"); 
    } 
});

1
您可以使用window.location尝试:
$(document).ready(function() {
    if(window.location !== "homepageurl"){
        $(".rightImage").last().append("<iframe src='http://images.findel-education.co.uk/EmailSignup/website_email_sign_up.html' width='180px'frameborder='0' height='250px'></iframe>");  
  }
});

1

检查 window.location,如果不匹配,则在 document.ready 中执行步骤。

    $(document).ready(function () {
        if (window.location != "exceptionURL") {

          $(".rightImage").last().append("<iframe src='http://images.findel-     education.co.uk/EmailSignup/website_email_sign_up.html' width='180px'frameborder='0' height='250px'></iframe>");
        }
    });

1
添加一个具有URL的过滤器。
  $(document).ready(function() {
      if(window.location.href!='http://yourhomepage'){
           $(".rightImage").last().append("<iframe src='http://images.findel-     education.co.uk/EmailSignup/website_email_sign_up.html' width='180px'frameborder='0' height='250px'></iframe>");  
       }
    });

0
您可以检查该URL:
$(document).ready(function() {
    if(window.location.href.indexOf('http://www.example.com') != 0) {
        $(".rightImage").last().append("<iframe src='http://images.findel-     education.co.uk/EmailSignup/website_email_sign_up.html' width='180px'frameborder='0' height='250px'></iframe>");  
    }
});

如果当前URL以'http://www.example.com'开头,它将不执行以下代码,否则它将附加代码。


0
你可以尝试这个:
$(function(){
    if(!location.pathName.contains('home')){ 
        $(".rightImage").last().append("<iframe .... height='250px'></iframe>");
    }
});

检查URL中是否包含与您的主页相关的特定单词。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接