简体   繁体   中英

Change img src with javascript

Hey guys I need some help

Im trying to change the src of an image tag but when change it using my code it adds the word file to the start of my string. The final result is file:///C:/xampp/.... instead of just C:/xampp/....

Here is my code

for(var i = 0; i < images.length; i++) {
        alert(images[i].src);
        srcList.push(images[i].src);
        var index = srcList[i].search("JavaBridge");
        tempPath.push(srcList[i].slice(index-1, srcList[i].length));
        finalPath.push("C:/xampp/tomcat/webapps"+tempPath[i]);
        document.getElementById(images[i].id).src = " "+finalPath[i];
        alert(images[i].src);
}

Thanks in Advance

For file manager the physical path is C:/xampp/.... but browser cannot use that directly. So, this is a default action and you cannot override it. Try to open a file with browser and see the url .. you will find file:/// appended before the path.

When you change this path to url.. the file:/// will not come.

One quick workaround is to use a server and load files using server.. You can easily setup server using apache (Xampp, Wamp) or Node (Gulp Task)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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