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?

0

3 Answers 3

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));
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.