Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript within an <a> tag, nested, quote problem

Here is simple <a> tag, which links to an exe file. The onClick JavaScript event redirects the user to another webpage after 3 seconds.

<a href="http://www.example.com/download.exe"
onClick="setTimeout('window.location="/downloading.html"',3000);return true;">
LINK</a>

So it doesn't work because there are too many nested quotes.

The first quotes "" are for the onClick function. The second quotes '' are for the SetTimeout function. I need third quotes for the window.location function. I've tried using both ' and " but none work. The above syntax fails.

I can solve it by refactoring the JavaScript into a function, but there are reasons why I cannot implement that. Is there a solution to this?

EDIT:

The answers below did not quite work, but led me to the correct solution:

onClick="setTimeout('window.location=\'/downloading.html\'',3000);return true;"
like image 644
soupagain Avatar asked Jan 21 '23 21:01

soupagain


2 Answers

You need to escape the quotes:

<a href="http://www.example.com/download.exe" onClick="setTimeout('window.location=\"/downloading.html\"',3000);return true;">Something</a>
like image 137
Matt Avatar answered Feb 01 '23 18:02

Matt


You need to escape the inner double quote with backslash.

Here is the example:

<a href="http://www.example.com/download.exe"
onClick="setTimeout('window.location=\"/downloading.html\"',3000);return true;"</a>
like image 23
Billy Avatar answered Feb 01 '23 18:02

Billy