LoginSignup
0
0

More than 5 years have passed since last update.

そういえば使ってなかったJSの機能をばらばらとまとめる

Last updated at Posted at 2017-09-29

選択している文字列の操作

Demo : http://codepen.io/mo4_9/pen/ObNjeR

window.getSelection()

参考
Rangeの活用とSelection

windowを開く

Demo : https://codepen.io/mo4_9/pen/gGRgWa

let newWin;
const WIDTH = 400;
const HEIGHT = 300;

window.name = "openerWin"; // オープン元ウィンドウの名前
newWindow = window.open("subwindow.html", "newWindow", `status=1,width=${WIDTH},height=${HEIGHT}`);
newWindow.resizeTo(screen.availWidth/2, screen.availHeight/2);
newWindow.moveTo(screen.availWidth/2 - WIDTH/2, screen.availHeight/2 - HEIGHT/2);

プロパティを動的に生成

Demo : https://codepen.io/mo4_9/pen/OxgWjX

Computed property names

ブラケットで囲う

let i = 0;

const member = {
  name: 'hoge',
  age: 20,
  [`memo${++i}`]: 'memo1',
  [`memo${++i}`]: 'memo2',
  [`memo${++i}`]: 'memo3'
}

console.log(member);

^ 消えたので画像も貼っておく

スクリーンショット 2017-09-29 21.50.35.png

HISTORY API

Demo : http://codepen.io/mo4_9/pen/XNOZyB

let count = 0;
const result = document.getElementById("result");

document.getElementById("btn").addEventListener("click", (e) => {
  if( window.history && window.history.pushState ){
    result.textContent = ++count;
    history.pushState(count, null, `${location.host}/${count}`)
    console.log(`${location.host}/${count}`)
  }
})

window.addEventListener("popstate", (e) => {
  count = e.state;
  result.textContent = count;
})

参考
【HTML5】HISTORY API「pushState/popState」を使って画面遷移しなくてもURLを変化させて、戻るボタンの挙動をカスタマイズする
pushState() + AJaxでスムーズなコンテンツの遷移

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