简体   繁体   English

用jQuery更新URL中的文件名

[英]update filename in a url with jquery

Wonder how I could do the following wit jquery - 想知道我怎么能做以下机智的jQuery-

I have a url pointing to an image -> http://www.website.com/folder/folder2/image_current.jpg (this value is stored in a variable) 我有一个指向图片的网址-> http://www.website.com/folder/folder2/image_current.jpg (此值存储在变量中)

I have variable that holds the value -> new_image.jpg 我有保存值的变量-> new_image.jpg

How can I with jquery update the full url (held in the variable) from 我如何用jquery从以下位置更新完整网址(保存在变量中)

http://www.website.com/folder/folder2/image_current.jpg http://www.website.com/folder/folder2/image_current.jpg

to

http://www.website.com/folder/folder2/new_image.jpg 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 此代码将文件名添加到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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM