Trying to remove the full URL that returns 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 would like to delete everything except filename.jpg and return it ahng_photos_upload_image. Reset everything to the last slash. How can I do this using jQuery?
window.send_to_editor = function(html) { imgurl = jQuery('img',html).attr('src'); jQuery('#ahng_photos_upload_image').val(imgurl); tb_remove(); }
You do not need jQuery for this, just the old JavaScript will do :)
alert('http://localhost/wordpress/wp-content/uploads/filename.jpg'.split('/').pop());
In your case:
var filename = imgurl.split('/').pop();
You can use regex to achieve this.
var file = imgUrl.replace(/^.*[\\\/]/, '');
.
, URL- , , :
var filename = imgurl.replace(/^.*\/([^/]*)$/, "$1");
: "imgurl" var, , , .prop() .attr(), jQuery 1.6 :
var
.prop()
.attr()
var imgurl = jQuery('img', html).prop('src');
jQuery :
var imgurl = jQuery(html).find('img').prop('src');
:
var filename = imgurl.substring(imgurl.lastIndexOf('/') + 1);
JS Fiddle demo.
imgurl.split('/').slice(-1);
: @Andy, pop(), , slice(-1).
pop()
slice(-1)
var filename = imgurl.split('/').slice(-1);
!
Note that if you do not know if you have fast forward or rewind, you are better off using the RE split version:
"path".split(/[\/\\]/).slice(-1)
Here is an answer that will work when your file name looks like. /file.jpg
var extension = fileName.slice((fileName.lastIndexOf(".") - 1 >>> 0) + 2); var baseName = fileName.replace(/^.*\/([^/]*)$/, "$1"); var path = fileName.replace(/(^.*\/)([^/]*)$/, "$1");