user1732192
user1732192

Reputation: 11

How to pass a url to a JavaScript function as a parameter?

I just want to pass the base url to a javaScript function. Function call is like as follows

<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth('.$bs_url.')"/>

It showing an error in the Error Console like

Error: SyntaxError: identifier starts immediately after numeric literal
Source File: `http://mywebsite/rand/`
Line: 1, Column: 15
Source Code:
nextMonth(http%3A%2F%2F192.168.1.254%2Frand%2F)

Upvotes: 0

Views: 1177

Answers (2)

Elbek
Elbek

Reputation: 3494

I think you can do just like this:

<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth($bs_url)"/>

Upvotes: 0

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

This line:

<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth('.$bs_url.')"/>

Should be written as:

<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth(\''.$bs_url.'\')"/>

to add string delimiters that don't muck up the double quotes of the HTML, and are escaped in your php script. I think I'm right in assuming this is actually part of a statement like:

echo '<img src="assets/images/c_arow_rite.jpg" onClick="nextMonth(\''.$bs_url.'\')"/>';

In which case, adding escaped single quotes is the answer.

Upvotes: 1

Related Questions