dpli36193 2016-08-22 21:00
浏览 115
已采纳

WordPress header.php使用错误的Jquery代码保留旧的已删除的<script>标记。 (没有启用缓存)

i have the following problem on this website: http://client1.studdo-media.nl/

when i open the dev tools for Chrome i see the following JS errors:

client1.studdo-media.nl/:8 Uncaught ReferenceError: $ is not defined
client1.studdo-media.nl/:23 Uncaught ReferenceError: $ is not defined
client1.studdo-media.nl/:47 Uncaught ReferenceError: $ is not defined

I know WordPress needs a noConflict Wrapper for JQuery to work with $.

I removed the wrong code in the header.php file and replaced it with correct, wrapped JQuery, and placed it in the body to make sure JQuery is loaded.

This is my header.php file right now

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">


<?php wp_head(); ?> 
</head>

    <body <?php body_class('blog-page'); ?>>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="/wp-content/themes/mercurius/assets/css/jquery.fullPage.css" />
<script type="text/javascript" src="/wp-content/themes/mercurius/assets/js/jquery.fullPage.js"></script>  

<script>
jQuery( document ).ready( function( $ ) {
        $('a[href*=#]:not([href=#])').click(function() {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.substr(1) +']');
            if (target.length) {
                $('html,body').animate({
                  scrollTop: target.offset().top
                }, 1500);
                return false;
            }
        });
        });
</script>
<script>
jQuery( document ).ready( function( $ ) {

    $('#someID').click(function(){
        $('html, body').animate({scrollTop:$(document).height()}, 'slow');
        return false;
    });

});
</script>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-81168606-1', 'auto');
  ga('send', 'pageview');
</script>

<!-- parallax header -->
<script>
  jQuery(window).scroll(function() {
    var scroll = jQuery(window).scrollTop();            
    jQuery(".carousel .fill").css("transform","translateY(" +  (scroll)/2  + "px)");
  });
</script>

    <!-- Header Section -->
    <header id="header"> 

note: everything under header opening tag is not relevant for this question and therefore not pasted here.

The correct code is loaded by WordPress right after the open tag.

I cant seem to find out why the old (wrong) code is still loaded into my website. Note:the wrong code seems to be added before the

<!DOCTYPE html>

see this image: wordpress script tag before

I have no caching enabled.

Edit: the scripts tags were loaded by another file in my themes folder, so it has not much to do with header.php.. (answer by lassemt)

  • 写回答

2条回答 默认 最新

  • doujiang1913 2016-08-22 21:38
    关注

    I don't have 50 reputation yet, so I couldn't comment this.. But I will try to explain to you whats wrong with the scripts.

    First, wordpress doesn't need noConflict wrapper unless it is jquery in backend (in the adminpanel).

    The problem is basically "cascading". Inspecting your code I noticed you have the same scripts loaded right after the head inside the head (they are duplicate). The reason you get these error messages is because the website are trying to run functions requiring jQuery before jQuery is loaded in your document.

    If you inspect your site and check inside the tag right after where google analytics are loaded you will see you have 3 different inline scripts loaded. The first e are scripts using jquery and are duplicate.

    It can look like these scripts come with wp_head() by looking at the code you shared with us, so maybe they are still in the functions.php?

    Edit with solution after checking out the theme

    In your theme if you look at the inc/includes/head.php file, you will see thats the file where the unwanted code is. This file is being included with the inc/init.php (it uses a function that includes all the files in the "includes" folder). Solution is to edit the head.php file and remove the scripts from it or just delete the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)