0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【JavaScript】タイトルの変え方

Posted at

JavaScriptを使用していてtitleタグの要素の取得苦戦したのでメモを残します。
サーバー側の処理はなくhtmlとjsだけで動かします。

機能としてはボタン押下時にブラウザのタイトルが切り替わるようにしたいと思います。

【titleタグの要素取得/変更】

//html
<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<button id="button" onclick="changeTitle()">ChangeTitle </button>

<script type="text/javascript" src="./JS/test.js"></script>
</body>
</html>

//JavaScript
titleタグの取得は「document.title」になるんですね。
変更する際は「document.titel='○○'」と代入するだけでOKです。

"use strict";

function changeTitle(){
    //test→change
    if(document.title=='test'){
        document.title='change';
        return;
    }

    //change→test
    if(document.title=='change'){
        document.title='test';
        return;
    }
}

以上、メモでした。

0
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?