update filename in a url with jquery
Wonder how I could do the following wit jquery -
I have a url pointing to an image -> http://www.website.c开发者_C百科om/folder/folder2/image_current.jpg (this value is stored in a variable)
I have variable that holds the value -> new_image.jpg
How can I with jquery update the full url (held in the variable) from
http://www.website.com/folder/folder2/image_current.jpg
to
http://www.website.com/folder/folder2/new_image.jpg
??
Try something like this:
var url = 'http://www.website.com/folder/folder2/image_current.jpg';
var new_image = 'new_image.jpg';
var new_url = url.replace(url.substring(url.lastIndexOf('/')+1), new_image);
this code is adding the file name at the start of url
var url = window.location.href;
var filename = "index1.php";
var newurl = url.replace(url.substring(url.lastIndexOf('/')+1), filename);
$(window).resize(function resize(){
if ($(window).width() < 800) {
window.location.replace(newurl);
}
}).trigger('resize');
so the url becomes
index1.phphttp://localhost/indictrans/
精彩评论