0
1

More than 3 years have passed since last update.

buttonタグをクリックした時にlocation.hrefが正常に機能しない問題の原因と解決

Last updated at Posted at 2020-01-06

私の環境だけだと思うんですが、不可解な現象に出くわしたので備忘録。

TL:DR 原因と解決

buttonタグはonclickを書いてもsubmitを無効にしないとスクリプトが実行されない。
そのため、<button type='button' onclick='...'>としなければならなかった。

経緯と再現

ボタンをクリックするとページジャンプ!をやりたいので、
location.href='(ジャンプ先)'を指定。
サクッとサンプルページを作りたいので、ローカルで環境を作ってVivaldi2.10...で実施。
ディレクトリ構造は以下の通り。

# tree
.
├── index.html
└── jump.html

たとえば、index.htmlからjump.htmlにリンクさせたいケースがあったとする。
aタグで指定した時はjump.htmlにジャンプできているとして、問題は

  • <input type='button' onclick='...'>ならjump.html
  • <button onclick='...'>ならindex.html ?

にジャンプする。
幾つかのサイトを参考にしたが、submitについて言及されておらず気付かないとハマるため、書き残しておく。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1