JavaScript file isn't executed
-
Hello there!
I suppose this is a pretty newbie question, but I just can’t figure it out… I would like to use a script to determine the window width dynamically in my template and load a different css. This is the script I’m using:
function adjustStyle(width) { width = parseInt(width); if (width < 1010) { $("#size-stylesheet").attr("href", "css/narrow.css"); } else { $("#size-stylesheet").attr("href", "css/wide.css"); } } $(function() { adjustStyle($(this).width()); $(window).resize(function() { adjustStyle($(this).width()); }); });
The code is in the file resolution-test.js
I’m using the following code in my header.php:
<?php function my_scripts_method() { wp_enqueue_script( 'resolution-test.js', get_template_directory_uri() . '/js/resolution-test.js', array( 'jquery' ) ); } add_action( 'wp_enqueue_scripts', 'my_scripts_method' ); ?> <link id="size-stylesheet" rel="stylesheet" type="text/css" href="https://***/css/wide.css">
I do know about the noConflict mode in WordPress, so I tried changing the JavaScript Code in the following way:
jQuery(document).ready(function(){ function adjustStyle(width) { width = parseInt(width); if (width < 1010) { $("#size-stylesheet").attr("href", "css/narrow.css"); } else { $("#size-stylesheet").attr("href", "css/wide.css"); } } $(function() { adjustStyle($(this).width()); $(window).resize(function() { adjustStyle($(this).width()); }); }); });
It still doesn’t work. What am doing wrong? Thanks in advance!
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘JavaScript file isn't executed’ is closed to new replies.