Venkat
Venkat

Reputation: 41

Insert a variable into a URL

I'm a newbie here.

I would like to insert a value from JS variable into a URL part of an tag.

<script language="javascript" type="text/javascript">
  var scrt_var = "xyz_nt"; 
</script

For example, take the above value and insert/replace in the below img src where it reads 'insert value here'.

 <TD>
   <img src="https://test.com/?target=alias(keepLastValue(aggregates.*.servera_*.abc-insert value here.errors)%2C%20'Errors')&preventCache=25308886"  height="250" width="620" />
 </TD>

Any help would be really appreciated.

Upvotes: 0

Views: 103

Answers (2)

L&#233;o R.
L&#233;o R.

Reputation: 2698

You can add an ID to your img and do something like:

<script>
    var scrt_var = "xyz_nt";
    var mylink = "http://your_url?your_var=" + scrt_var;
    document.getElementById('image').src = mylink;
</script>

Upvotes: 4

gurvinder372
gurvinder372

Reputation: 68393

You can use regex replace

var scrt_var = "xyz_nt"; 
var image = document.querySelector("td img");
image.src = image.src.replace( /insert value here/, scrt_var );

Upvotes: 1

Related Questions