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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探