LoginSignup
1
0

More than 5 years have passed since last update.

jQueryを使用してinputで入力したテキストをブラウザバック or リロードした際に表示させたい

Last updated at Posted at 2019-05-07

記述内容

HTML

<p>入力:<input type="text" name="test" size="40"></p>
<p><input type="submit" value="格納"></p>
<div class="add_txt"></div>

JavaScript

$(function(){
    $('input[type="submit"]').click(function(){
        let url = location.href;
        let target = $('input[name="test"]').val();
        history.pushState(target, '', url);
    })
});

$(window).load(function(){
    if ('pushState'in history) {
        $('.add_txt').text("前に入力したテキスト→"+history["state"]);
    }
});

※jQueryのバージョンは環境によって変更が必要かも。

Github Repository

1
0
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
1
0