luftikus143
luftikus143

Reputation: 1285

Replace rear part of image source string with different parameter

I have an image (which is actually a webservice map), where, upon a change of user chosen parameters, the bounding box must be changed and the image updated.

It's working if I replace the whole image tag:

document.getElementById("dropzone1").innerHTML = "<img style='margin-top: 0' 
  src='http://neowms.sci.gsfc.nasa.gov/wms/wms?
  SERVICE=WMS&VERSION=1.1.0&REQUEST=GetMap&FORMAT=image/png&TRANSPARENT=true&
  LAYERS=MOD13A2_E_NDVI&SRS=EPSG:4326&STYLES=&WIDTH=500&HEIGHT=400&
  BBOX=10.163726806640625,49.10217838413516,10.240631103515625,49.14172346635712'>";

But I'd like to only replace the rear part, the BBOX data with a variable "extent":

  str.replace("10.163726806640625,49.10217838413516,10.240631103515625,49.14172346635712", extent")

Is there a simple way of doing this? The number of decimals vary.

Thanks for any hints!

Upvotes: 0

Views: 42

Answers (3)

Farzin Kanzi
Farzin Kanzi

Reputation: 3435

Its better your image has an id to access that, Or we can access to the element (image) by it's index in parent. For example if your image is the first element of its parent0 div id="dropzone1":

var myImage = document.getElementById("dropzone1").children[0];
var oldSrc = myImage.src;
var newboundingBox = "NEW BOUNDING BOX";
var srcArray = oldSrc.split('BBOX=');
srcArray[1] = newboundingBox;
var newSrc = srcArray.join('BBOX=');


myImage.src = newSrc ;

Upvotes: 0

CaptainHere
CaptainHere

Reputation: 698

This is a simple solution.

var n = str.lastIndexOf('BBOX');
var o=str.lastIndexOf('>');
var result = str.substring(n + 1,o-1);

Then you can replace the result with extent.

str.replace(result, extent);

Upvotes: 0

Stefano Zanini
Stefano Zanini

Reputation: 5916

You can use regular expressions, like

yourString.replace(/BBOX=[\d\.,]+/, 'BBOX=' + yourNewBBox);

That regex will search for substrings composed by 'BBOX=' followed by any combination of digits, dots and commas.

Upvotes: 1

Related Questions