user3422998
user3422998

Reputation: 105

Put variable into url

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.

Upvotes: 0

Views: 189

Answers (4)

chfumero
chfumero

Reputation: 1147

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);
    }

Upvotes: 0

tarnelope
tarnelope

Reputation: 1

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;

Upvotes: 0

blex
blex

Reputation: 25659

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

Upvotes: 1

macl
macl

Reputation: 995

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;

Upvotes: 0

Related Questions