Trying to remove the full url that is being returned to imgurl:
Usually returns something like http://localhost/wordpress/wp-content/uploads/filename.jpg
or http://localhost/wordpress/wp-content/uploads/images/filename.jpg
I’d like to strip off everything except filename.jpg and return it to
ahng_photos_upload_image. Strip off everything to the last forward-slash.
How can I do that with Jquery?
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#ahng_photos_upload_image').val(imgurl);
tb_remove();
}
You don’t need jQuery for that, just plain old JavaScript will do 🙂
In your case:
you can use a regular expression in order to achieve this..
Now the file would consist of only the file name ..
If you’re pretty confident that the URLs don’t have funny stuff like hashes or parameters, a regex like this would do it:
Also: don’t forget to declare “imgurl” with
var
, and you should probably use.prop()
instead of.attr()
if your version of jQuery is 1.6 or newer:Also jQuery internally turns the two-argument form of the function into this:
so you might as well code it that way.
One further option:
JS Fiddle demo.
Here you have
Good luck!
Try this one:
Edit: Look at the version of @Andy who uses the
pop()
method, the latter being faster thanslice(-1)
.Note that if you don’t know if you have forward or backward slashes, you are better off using the RE version of split:
Here is an answer that will work when your file name is like ./file.jpg