惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - RobotTech

[Z] C#程序中设置全局代理(Global Proxy) [Z] SQL SERVER 的前世今生--各版本功能对比 关于.NET编译的目标平台(AnyCPU,x86,x64) (转) [转] HTTP Headers 入门 [转] 一个小时学会Git 用命令编译 js事件之event.preventDefault()与event.stopPropagation()用法区别 [转] The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing [转] Visual Studio Code behind a proxy [转] js == 与 === 的区别 [转]说说C#的async和await Oracle 12c SQL Server死锁 Initialize the Storage Emulator by Using the Command-Line Tool Microsoft Fakes Identifier 'Logic.DomainObjectBase._isNew' is not CLS-compliant SSD TRIM Visual Studio 2013 prerequisites 怎样用UltraISO制作U盘系统安装盘
[Z] 从Uncaught SyntaxError: Unexpected token ")" 问题看javascript:void的作用
RobotTech · 2018-10-25 · via 博客园 - RobotTech

https://blog.csdn.net/hongweigg/article/details/78094338

问题
    在前端编程中,突然出现Uncaught SyntaxError: Unexpected token “)”的错误,行号1。这个错误真的很隐晦,开始还以为是其他JS文件的错误,结果发现错误原来就存在当前问件中。错误场景是这样的,使用<a>X</a>作为关闭窗口的按钮,代码如下:

   <a href="javascript:void()" id="__sc_closeBtn" class="closeBtn" title="close">&nbsp;X&nbsp;</a>

点完关闭后,就发生了开头提到的错误。

分析
    该HTML段代码是使用JavaScript脚本动态插入到DOM树中的,当时并没有怀疑到是这个地方的问题,直到搜索相似错误时,发现有人也碰到了类似的问题。原来是<a>标签中脚本缺少了一个0。

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>a标签javascript:void(0)与javascript:void()区别</title>
</head>

<body>
<a href="javascript:void(0)" id="__sc_closeBtn" class="closeBtn" title="close"> X </a>

<a href="javascript:void()" id="__sc_closeBtn" class="closeBtn" title="close"> X </a>
</body>

</html>

点第1个“X”时,不报错,点第2个“X”时报错,而且错误位置为1行7列,可见,问题出在<a>标签中的脚本上。
引申
为什么不加0就报错呢?看一下void的定义:

void 运算符
避免表达式返回值。

void expression

expression 参数是任意有效的 JScript 表达式。

void 运算符对表达式求值,并返回 undefined.在希望求表达式的值,但又不希望脚本的剩余部分看见这个结果时,该运算符最有用。

以上为void的含义,void 实际上是一个求职表达式,类似于eval,但不返回任何值。

在<a>标签中,href属性可以执行javascript,但若有返回值,则会发生页面跳转,页面显示值为javascript后数据:

<a href="javascript:1" id="__sc_closeBtn" class="closeBtn" title="close">javascript:1</a>
<a href="javascript:'test is ok'" id="__sc_closeBtn" class="closeBtn" title="close">javascript:'test is ok'</a>
这个不会发生跳转,但是会弹出提示框:
<a href="javascript:alert(2)" id="__sc_closeBtn" class="closeBtn" title="close">javascript:alert(2)</a>
当我们利用<a>标签不是为了页面跳转,而是为了屏蔽页面跳转,那么void函数就派上用场了:

<a href="javascript:void 0" id="__sc_closeBtn" class="closeBtn" title="close">javascript:void 0</a>
实际上,void不使用挎号也完全是可行的,使用括号是为了让代码编写得更规范。
void后接任何合法的javascript代码,均能达到不返回值、页面不跳转的效果,但显然 void 0 是最简洁的写法。

注意:

如果是表达式,则需要加上括号:

<a href="javascript:void 9+19" id="__sc_closeBtn" class="closeBtn" title="close">javascript:void 9+19</a>

<a href="javascript:void (9+19)" id="__sc_closeBtn" class="closeBtn" title="close">javascript:void(9+19)</a>

<a href="javascript:void eval(9+19)" id="__sc_closeBtn" class="closeBtn" title="close">javascript:void eval(9+19)</a>
第1个返回NaN,第2、3个不返回值。