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

推荐订阅源

T
Threatpost
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
T
Tenable Blog
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
P
Privacy & Cybersecurity Law Blog
Know Your Adversary
Know Your Adversary
T
The Exploit Database - CXSecurity.com
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
Intezer
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
Simon Willison's Weblog
Simon Willison's Weblog
The Register - Security
The Register - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
Lohrmann on Cybersecurity
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
H
Help Net Security
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
S
Schneier on Security
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
P
Privacy International News Feed
S
Secure Thoughts
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recorded Future
Recorded Future
C
Cybersecurity and Infrastructure Security Agency CISA
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
博客园 - Franky
T
Tor Project blog
G
GRAHAM CLULEY
博客园 - 【当耐特】
Jina AI
Jina AI
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
A
About on SuperTechFans
Hacker News - Newest:
Hacker News - Newest: "LLM"

Wayne的博客

从 AI Coding 到 Agent Loop:2026H1 研发工具演进趋势 Awesome AI Prompts:提示词收集手册 | Wayne的博客 大模型缓存技术工程指南(下):面向缓存命中的 Prompt 与 Agent 工程实践 大模型缓存技术工程指南(上):从价格信号到推理缓存机制 | Wayne的博客 面向 AI / Agent 友好的 CLI 开发建议 【笔记】Harness Engineering(驾驭工程) | Wayne的博客 【工具】ZeroClaw配置项(config.toml)说明 | Wayne的博客 【工具】ZeroClaw配置项(config.toml)说明 | Wayne的博客 Skills 技术深度解析:AI Agent 能力扩展的新范式 | Wayne的博客 【笔记】浏览器PNA/LNA策略——记一次iframe中CORS报错的问题排查经历 | Wayne的博客 【工具】spec驱动开发解析 | Wayne的博客 Agent设计模式 | Wayne的博客 【工具】2025年AI Coding部分产品技术分析——ClaudeCode和CodexCLI | Wayne的博客 【工具】设计-开发-测试的AIGC产品清单梳理 | Wayne的博客 近期关于AI浪潮下的搜索引擎、SEO和GEO思考 | Wayne的博客 【工具】AI Common Notify:统一 AI 编程工具通知服务 21st.dev:让AI生成的页面告别"塑料感"的专业解决方案 | Wayne的博客 【笔记】Figma和AIGC(持续) | Wayne的博客 【笔记】State-Of-Ai 报告信息 | Wayne的博客 【笔记】web 黑夜模式通用适配方案 | Wayne的博客 【笔记】19届阿里D2终端技术大会纪要 | Wayne的博客 【笔记】Lovable提示词指南 | Wayne的博客 【调研】AI 编程工具WindSurf使用技巧——WindsurfRules配置 | Wayne的博客 【AI】【笔记】MCP 协议:连接 AI 模型与外部世界的桥梁 | Wayne的博客 AI 前端编程工具的一个得力助手——CopyCoder | Wayne的博客
【笔记】location.href 和 location.assign 进行页面 URL 跳转
2026-02-23 · via Wayne的博客

location.hreflocation.assign()进行页面 URL 跳转

两者都可以触发页面导航(跳转到新 URL)。

在常见页面跳转场景中,location.href = urllocation.assign(url) 的行为基本一致:

  • 都会导航到目标 URL
  • 都会新增一条历史记录(通常可后退)

MDN 对 href 的描述是:它返回完整 URL,并且允许更新;设置 href 会导航到该 URL。
MDN 对 assign() 的描述是:它会让窗口加载并显示指定 URL 的文档。

先看对照表

API 是否新增历史记录 是否文档级跳转 常见场景
location.href = url 常规跳转
location.assign(url) 常规跳转(强调“执行导航”语义)
location.replace(url) 登录后跳转、不希望返回上页
location.reload() 不涉及 是(刷新当前页) 主动刷新当前页面

location.href属性

location.href 是一个可读写属性,表示当前页面的 URL。当我们改变它的值时,浏览器会跳转到新的页面。

1
2

location.href = 'https://www.baidu.com';

特点

  • 语法简洁:属性赋值,代码短
  • 历史记录:会新增历史条目,可后退
  • 兼容性好:所有主流浏览器都支持

示例

1
2
3
4
5

console.log(location.href);


location.href = '/about';

location.assign()方法

location.assign()Location 接口的方法,用于加载给定 URL。

在常见页面跳转场景中,它与 location.href 赋值的效果基本一致。

1
2
3
4
5

location.assign('https://www.baidu.com');


location.assign('/about');

特点

  • 语义明确:方法调用更像“执行一次导航”
  • 历史记录:会新增历史条目,可后退
  • 参数形式:可传绝对 URL、相对 URL、hash、URL 对象等可转为 URL 的值
  • 对测试友好location.assign() 是方法,单元测试里更容易 mock / spy,比如在 Jest 里可以直接断言它是否被调用、参数是否正确

从 caniuse 来看,兼容性和 href 一样,所以兼容性角度来看也不用顾虑。

API 说明(异常)

  • 由于安全限制无法导航时,可能抛出 DOMException(如 SecurityError
  • 传入无效 URL 时,可能抛出 DOMException(如 SyntaxError

如何选择使用?

location.href vs location.assign()

对一般开发者来说,这两个 API 在日常跳转里差异很小,主要按团队风格选择:

  • 使用 location.href:更短、更常见
  • 使用 location.assign():语义更明确
1
2
3

location.href = '/products';
location.assign('/products');

此外,我们知道 location.href 也可作为可读属性,因此从能力上可以说比 assign 更多一点。

StackOverflow 上在十几年前也有关于两个 API 的 pk 讨论(location.href property vs. location.assign method),但结果是“主流共识:功能上基本等价”、““性能差异”被讨论很多,但结论是:有争议且不重要”。

*不希望保留历史记录:location.replace()

1
2

location.replace('/dashboard');

*需要刷新页面:location.reload()

1
2

location.reload();

location.reload(true) 是历史写法,true 参数并非跨浏览器标准行为。实际项目中建议只使用 location.reload()

*SPA 场景提示

在 React/Vue 等单页应用中,站内跳转通常优先使用路由 API(如 router.push)。

location.* 会触发文档级导航,通常会导致整页重新加载。

为什么 location.assign() 使用相对较少?

主要是工程习惯问题:

  • location.href 更短
  • 历史示例更多,团队更熟悉
  • 常见场景下两者效果接近

总结

单从 URL 跳转角度来看,location.hreflocation.assign() 行为基本一致,团队中只要保持习惯统一即可,如果要考虑测试场景,location.assign() 在相对更好 mock。


相关链接