javascript - set css image from current folder using jquery script page -


i have external javascript page located under ../scripts/cbox/ folder parent. there image located in same folder. want set background-image control using jquery there. when use code, sets background-image path localhost:7905/ddl_arrow.png localhost:7905 asp.net development server.

 function createautocbox(boxctrl) {      $(boxctrl).css('background-image', 'url("ddl_arrow.png")');      $(boxctrl).css('background-repeat', 'no-repeat');     $(boxctrl).css('background-position-x', '99%');     $(boxctrl).css('background-position-y', '-2px');      $(boxctrl).mousemove(jqautocompletemousemove);     $(boxctrl).keyup(jqautocompletekeyup);     $(boxctrl).mousedown(jqautocompletemousedown); } 

there no way "get current script's path on server", since js done on client-side. there no easy way thinking of.

there ways work around this, , of them based on same principle: organise files - each resource should url. think it: if cannot reliable tell ddl_arrow.png stored, neither can browser.

i think best solution put images inside [img] folder server root. means can reference images way: url(/img/ddl_arrow.png). no matter js, css or html file needs images, make sure reference images preceding slash. of course, applies not images, other assets/resources - fonts, videos, audio, , html, css, js files themselves. every file server serves should referenced way.

there other hacks involving nasty, messy stuff using server-side scripts print location of js file being fetched right js file, i'd recommend stay far away these methods.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -