Genadinik
Genadinik

Reputation: 18649

How to make a URL in JavaSctipt

I currently have this JavaScript:

new_string = "<p>Problem name: <a href=\"http://www.problemio.com/problems/problem.php?problem_id=\" + problem_id + ">" + title + "</a></p>";

Where the title and problem_id are variables with a string and an id respectively.

How should I approach quotes when I have such situations as I am facing now?

Thanks!

Upvotes: 1

Views: 170

Answers (5)

Nicolas BADIA
Nicolas BADIA

Reputation: 5852

You can do this:

new_string = "<p>Problem name: <a href=\"http://www.problemio.com/problems/problem.php?problem_id=" + problem_id + "\">" + title + "</a></p>";

Or this:

new_string = '<p>Problem name: <a href="http://www.problemio.com/problems/problem.php?problem_id='+ problem_id +'">' + title + '</a></p>';

Upvotes: 0

James Montagne
James Montagne

Reputation: 78770

I believe you just have one quote out of place:

new_string = "<p>Problem name: <a href=\"http://www.problemio.com/problems/problem.php?problem_id=" + problem_id + "\">" + title + "</a></p>"

Upvotes: 1

usoban
usoban

Reputation: 5478

' for string, " for quotes inside string. I'm doing it like this, and I've seen most javascript projects do it that way too.

Upvotes: 2

Joe
Joe

Reputation: 82654

Just end it with a quote:

var variable = 0;
var string = "\"quoted\"" + variable + "\"quotes everywhere\""; // "quoted"0"quotes everywhere"

Upvotes: 2

alecananian
alecananian

Reputation: 1152

You've just made a few mistakes with your quotation marks and slashes. It's best to use both single and double quotes to keep track:

new_string = "<p>Problem name: <a href='http://www.problemio.com/problems/problem.php?problem_id=" + problem_id + "'>" + title + "</a></p>";

However, if you'd like to go the slash route:

new_string = "<p>Problem name: <a href=\"http://www.problemio.com/problems/problem.php?problem_id=" + problem_id + "\">" + title + "</a></p>";

Upvotes: 2

Related Questions