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

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

c# - Search and Add Comment with OpenXML for Word -