简体   繁体   中英

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)

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.

我认为您可以这样做:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM