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.

1

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

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;

Comments

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

1 Comment

what if the values are changed for username and password. you should pass in variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.