Channel Advisor / Ebay Image quality Issue

2019-08-25 05:10发布

问题:

If I have a image src on my HTML as follow:

http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/FrMAAOSweW5U3QvN/$_12.JPG?set_id=880000500F

and I want to change all $_12.JPG to $_57.JPG in order to get the better image quality like below:

http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/FrMAAOSweW5U3QvN/$_57.JPG?set_id=880000500F

How do I do this with Javascript so that it replaces all $_12.JPG on my HTML with $_57.JPG?

Thank you in advance!!

回答1:

You can change the URL using .replace() for example:

document.getElementById('TheImage').src.replace('$_12.JPG','$_57.JPG');

Here is a small demo so you can see this in action.


Demo Source Code


function URLswap(){
    //--Set image variable
    var curlink=document.getElementById('ebay');
    //--Replace unwanted sting with new string.
    var Newlink=curlink.src.replace('$_12.JPG','$_57.JPG');
    //---Set new src
    curlink.src=Newlink;
    //-- Display new src (text) in div
    document.getElementById('currentlink').innerHTML='New Src= '+Newlink;
}
<button onclick="URLswap()">Change</button><br>
<div id="currentlink">Current Src= http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/FrMAAOSweW5U3QvN/$_12.JPG?set_id=880000500F</div>
<img id="ebay" src="http://i.ebayimg.com/00/s/NzgwWDc4MA==/z/FrMAAOSweW5U3QvN/$_12.JPG?set_id=880000500F"/>

For multiple image you can give those images a class and use a for loop to apply this to all images with that class but since you don't have any source code in your question or any mention of id / class I'm just keeping the answer as basic as the question.

I hope this helps. Happy coding!