0

A have an image address of my IP cam:

snapshot.cgi?user=#USERNAME&pwd=#PASSWORD

I need to put those two variables below in place #USERNAME and #PASSWORD:

var user = "test";
var pass = "test";

Finish result must be:

snapshot.cgi?user=test&pwd=test

I know there is a way to do that but i don't know how. The address of image need to stay exactly like it is as I am getting this from external source.

user3422998
  • 105
  • 1
  • 7

4 Answers4

1

You could use the .replace function:

var originalLink = "snapshot.cgi?user=#USERNAME&pwd=#PASSWORD",
    user = "test",
    pass = "test";

var newLink = originalLink.replace("#USERNAME", user).replace("#PASSWORD", pass);

JS Fiddle Demo

blex
  • 24,941
  • 5
  • 39
  • 72
0

To create a variable with that url, the quick code would be:

var user = "test";
var pass = "test";
var url = "snapshot.cgi?user="+user+"&pwd="+pass;
macl
  • 975
  • 9
  • 15
0

Is your image url address saved as a string url? If so, then you can do the following...

var user = "test";
var pass = "test";
var url = "snapshot.cgi?user=#" + user + "&pwd=#" + pass;
0

You should use the replace method of the string.

function getNewUrl (userName, password) {

        return "snapshot.cgi?user=#USERNAME&pwd=#PASSWORD"
                .replace("#USERNAME", userName)
                .replace("#PASSWORD", password);
    }
chfumero
  • 1,137
  • 2
  • 13
  • 26