0

I would like to get the image name out of the address.

This is the value I with the JavaScript:

http://localhost:51557/img/column-sortable.png

document.getElementById("ctl00_contentHolder_iSortColumn").value = columnNumber;
            alert(imageName);

Whats the best way to get column-sortable.png out of the string?

Freddy
  • 960
  • 1
  • 20
  • 46

3 Answers3

3

As long as there's never anything after the image name in the URL (no query string or hash) then the following should work:

var str = "http://localhost:51557/img/column-sortable.png";
alert(str.substring(str.lastIndexOf('/') + 1));
alnorth29
  • 3,525
  • 2
  • 34
  • 50
2

Please refer to the split function:

var url = http://localhost:51557/img/column-sortable.png;
var elementArray = url.split('/');
var imageName = elementArray[elementArray.length - 1];

JSFiddle

1

If you want to try using Regex, check this out.

var imgURL = "http://localhost:51557/img/column-sortable.png";
var imageName = imgURL.replace( /^.*?([^/]+\..+?)$/, '$1' );
alert(imageName);
Praveen
  • 55,303
  • 33
  • 133
  • 164