Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
    <title>ボタンクリックイベント</title>
</head>
<body>
<h1>JavaScriptからのAPI実行</h1>
<p>GitHubの総リポジトリ数を検索します。<br>検索キーワードを入力してください。</p>
<input type="text" id="searchword">
<input type="button" value="検索" onclick="searchGitHub()">
<p id="sysMsg" style="display:none;"></p>
<p id="fetchMsg"></p>
<p id="xhrMsg"></p>
  </body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                function searchGitHub() {
        // メッセージ初期化
        document.getElementById("fetchMsg").innerHTML = "";
        document.getElementById("xhrMsg").innerHTML = "";
        var sysMsgElement = document.getElementById("sysMsg");
        sysMsgElement.innerHTML = "";
        sysMsgElement.style.color = "red";
        sysMsgElement.style.display = "none";
        // 入力値取得
        var inputValue = document.getElementById("searchword").value;
        // 未入力判定
        if (inputValue == "") {
            sysMsgElement.style.display = "block";
            sysMsgElement.innerHTML = "未入力です";
            return;
        }
        callapi_fetch(inputValue);
        callapi_xhr(inputValue);
    }

    function callapi_fetch(inputValue) {
        var apiUrl = "https://api.github.com/search/repositories?q=" + inputValue;
        // GETリクエストを送信
        fetch(apiUrl)
            .then(response => {
                // レスポンスが正常に受信された場合
                if (response.ok) {
                    return response.json(); // JSONデータを取得
                } else {
                    throw new Error('APIリクエストに失敗しました。');
                }
            })
            .then(data => {
                // レスポンスデータを処理
                console.log(data.total_count);
                document.getElementById("fetchMsg").innerHTML = "[fetch処理]: " + inputValue + "のリポジトリ総数は、" + data.total_count + "件です。";
            })
            .catch(error => {
                // エラーハンドリング
                console.error(error);
            });
    }

    function callapi_xhr(inputValue) {
        // XMLHttpRequestオブジェクト作成
        var xhr = new XMLHttpRequest();
        // リクエスト設定(メソッド、URL、非同期フラグ設定)
        xhr.open("GET", "https://api.github.com/search/repositories?q=" + inputValue, true); // 
        // リクエスト送信
        xhr.send();
        // レスポンス処理
        xhr.onreadystatechange = function () {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                if (xhr.status === 200) {
                    // 戻り値は文字列
                    var responseText = xhr.responseText;
                    try{
                        var jsonData = JSON.parse(responseText);
                        console.log(jsonData.total_count);
                        document.getElementById("xhrMsg").innerHTML = "[XMLHttpRequest処理]: " + inputValue + "のリポジトリ総数は、" + jsonData.total_count + "件です。";
                    } catch (error) {
                        console.error('JSONパースエラー:', error);
                    }
                } else {
                    // エラーハンドリング
                    console.error('Request failed with status:', xhr.status);
                }
            }
        };
    }
              
            
!
999px

Console