0

How can you create an image src automatically named after the current Url? For instance xxy.html creates an img src for xxy.jpg , file paths will be the same for the .html and .jpg files and all images will be .jpg and all pages .html. Am not using php, but can implement javascript, jquery or css solutions!

In case that didn't make sense I just need a code that reads the current page url, and enables img src="(name of current page -".html") .jpg

  • 1
    I am not sure what response do you expect here. I doubt replacing a part of the url is within css' capabilities and I don't know what do you mean by `query`. Otherwise all you have to do is to do simple substitution in a string and it is easiest to do that using regexes in any language. – Ivaylo Strandjev Jan 12 '13 at 08:04
  • You can use `window.location` in javascript to get the current page URL. If you mean javascript then [read more here](http://stackoverflow.com/questions/1034621/get-current-url-with-javascript) – djthoms Jan 12 '13 at 08:06
  • Sorry i meant I can use jquery but autocorrect always takes the J off. I don't need to replace a part of the url. I just need a code that reads the url string, and enables – Gravegil Axshark Jan 12 '13 at 08:11

2 Answers2

0

A quick and dirty way:

<script>document.write('<img src="' + location.pathname.replace(/\.html$/, '.jpg') + '">')</script>

Put that in your HTML where you want your image to go.

robertklep
  • 198,204
  • 35
  • 394
  • 381
0

During page load ;

`<img src="" id=img1>`

after page load; at end of the page :

<script>
document.getElementById('img1').src= window.location.replace(".html", ".jpg");
</script>

I havent tested the code. But it gives you good idea.

Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98