Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  85] [ 3]  / answers: 1 / hits: 128619  / 14 Years ago, mon, march 7, 2011, 12:00:00

I added custom script:



wp_enqueue_script('functions', get_bloginfo('template_url') . '/js/functions.js', 'search', null, false);


It works great, except in the functions.js I have:



Reset.style.background = url('../images/searchfield_clear.png') no-repeat top left;


This used to work before, until I changed to pretty permalinks and .htaccess



The folder hierarchy is like:




themename/js themename/images (the images and js folder are in themename folder)




I tried ../images - ./image - /images



Normally it should go back 1 level wherever the js file is located....



I don't want to use full path.



Is there another way that WordPress can recognize in the javascript file to have the correct path?



Currently I am just confused what I am doing wrong.


More From » wordpress

 Answers
59

You could avoid hardcoding the full path by setting a JS variable in the header of your template, before wp_head() is called, holding the template URL. Like:



<script type=text/javascript>
var templateUrl = '<?= get_bloginfo(template_url); ?>';
</script>


And use that variable to set the background (I realize you know how to do this, I only include these details in case they helps others):



Reset.style.background =  url('+templateUrl+/images/searchfield_clear.png') ;

[#93395] Friday, March 4, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyreek

Total Points: 421
Total Questions: 115
Total Answers: 102

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
;