最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript within an <a> tag, nested, quote problem - Stack Overflow

programmeradmin5浏览0评论

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=".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;"

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./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;"
Share Improve this question edited Jun 16, 2010 at 17:21 soupagain asked Jun 16, 2010 at 16:41 soupagainsoupagain 1,1335 gold badges16 silver badges32 bronze badges 1
  • Can you write your code in here. So it would be easy for people to go through – sushil bharwani Commented Jun 16, 2010 at 16:43
Add a ment  | 

2 Answers 2

Reset to default 6

You need to escape the quotes:

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

You need to escape the inner double quote with backslash.

Here is the example:

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

评论列表(0)

  1. 暂无评论