最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Fetching a image in server context path from javascript code - Stack Overflow

programmeradmin5浏览0评论

I have a spring based web application MyWebapp built using maven and deployed on Websphere 6.1

The folder structure is:

MyApp --> src ---> main --->

The main folder is further having resources and webapp folders.

webapp folders is having other folders like images, theme, jscript, JSP, META-INF, WEB-INF

images folder is having icons folder with say example.png

So fetching example.png on localhost as:

http://localhost:9080/MyWebapp/images/icons/example.png

succeeds.

In jscript folder I have a sample.js javascript file where some functions are defined.

I am importing this javascript file in JSP pages as:

<script src="<%=request.getContextPath()%>/jscript/sample.js" type="text/javascript" language="Javascript"></script>

This javascript file is having a function which tries to fetch image as below:

iconFile = '../images/icons/search_result.png';  
anchor.style.backgroundImage = 'url(' + iconFile + ')'; 
anchor.style.backgroundRepeat = 'no-repeat';        
anchor.style.backgroundPosition = '1px 2px';        
anchor.className = 'toplevel-tab';

The plete function basically tries to place a icon before some text in JSP.

The code gets parsed. However, the image does not get displayed.

Running the code independently on a simple html with the png images in the same folder as html and javascript files succeeds. Here i will just have iconFile = "search_result.png"

So, it is not code issue.

Issue is that the image is not getting located or the server is unable to find the image in above javascript code.

What am I doing wrong ? How can I solve it ?

The answer for which I accepted earlier does not work. So please do not downvote this question as a duplicate one.

Also I am working on restricted environment where access to programs like Tail will not work.

Changing

iconFile = '../images/icons/search_result.png';

to

iconFile = '/images/icons/search_result.png';

also does not work!!

Thanks for reading!

I have a spring based web application MyWebapp built using maven and deployed on Websphere 6.1

The folder structure is:

MyApp --> src ---> main --->

The main folder is further having resources and webapp folders.

webapp folders is having other folders like images, theme, jscript, JSP, META-INF, WEB-INF

images folder is having icons folder with say example.png

So fetching example.png on localhost as:

http://localhost:9080/MyWebapp/images/icons/example.png

succeeds.

In jscript folder I have a sample.js javascript file where some functions are defined.

I am importing this javascript file in JSP pages as:

<script src="<%=request.getContextPath()%>/jscript/sample.js" type="text/javascript" language="Javascript"></script>

This javascript file is having a function which tries to fetch image as below:

iconFile = '../images/icons/search_result.png';  
anchor.style.backgroundImage = 'url(' + iconFile + ')'; 
anchor.style.backgroundRepeat = 'no-repeat';        
anchor.style.backgroundPosition = '1px 2px';        
anchor.className = 'toplevel-tab';

The plete function basically tries to place a icon before some text in JSP.

The code gets parsed. However, the image does not get displayed.

Running the code independently on a simple html with the png images in the same folder as html and javascript files succeeds. Here i will just have iconFile = "search_result.png"

So, it is not code issue.

Issue is that the image is not getting located or the server is unable to find the image in above javascript code.

What am I doing wrong ? How can I solve it ?

The answer for https://stackoverflow./a/8298652/887235 which I accepted earlier does not work. So please do not downvote this question as a duplicate one.

Also I am working on restricted environment where access to programs like Tail will not work.

Changing

iconFile = '../images/icons/search_result.png';

to

iconFile = '/images/icons/search_result.png';

also does not work!!

Thanks for reading!

Share Improve this question edited May 23, 2017 at 12:11 CommunityBot 11 silver badge asked Dec 13, 2011 at 10:36 VickyVicky 17.4k55 gold badges152 silver badges244 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You just have to understand how relative paths work. Even if the path is in a JavaScript file, the path is not relative to the location of this JS file, but it's relative to the URL of the HTML page being displayed in the browser.

So, if the URL of the page executing this javascript code is

http://foo.bar./myWebApp/zim/boom/tchak.html

and the URL of the image is

../images/icons/search_result.png

The absolute URL of the image will be

http://foo.bar./myWebApp/zim/boom/../images/icons/search_result.png 

which is the same as

http://foo.bar./myWebApp/zim/images/icons/search_result.png 

An absolute path like /images/icons/search_result.png is also resolved from the root of the web server, and not the root of the webapp (the browser doesn't know what a Java webapp is and doesn't care). So it's resolved as

http://foo.bar./images/icons/search_result.png

You would need to prepend the context path to the path to make it really absolute:

<%=request.getContextPath()%>/images/icons/search_result.png

or, without scriptlets:

${pageContext.request.contextPath}/images/icons/search_result.png

You need to give your javascript an awareness of the path to the root of your application, as this will change on context. Start by declaring a global variable, such as:

<script>
    var siteroot = "<%=request.getContextPath()%>";
</script>

Then, you are ready to use it later, such as:

iconFile = siteroot + '/images/icons/search_result.png';  
发布评论

评论列表(0)

  1. 暂无评论