161122、BOM 操作寫法樣本

來源:互聯網
上載者:User

標籤:amp   tar   改變   doc   query   name   頁面跳轉   hostname   interval   

瀏覽器相關資訊

// 瀏覽器資訊

navigator.userAgent // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36

// 運行瀏覽器的作業系統平台

navigator.platform // MacIntel

 

瀏覽記錄曆史

// 前進

history.forward()

// 後退

history.back()

 

url 資訊

頁面跳轉

// 當前頁面會記入瀏覽記錄曆史

location.href = ‘http://www.baidu.com‘

// 當前頁面不會記入瀏覽記錄曆史

location.replace(‘http://www.baidu.com‘)

 

頁面重新載入

location.reload()

 

獲得當前 url 的資訊

// 假設當前 url 是 http://192.168.31.194:8000/jquery/index.html?a=3&c=aa#b

 

location.href // 完整的url

location.hostname // 192.168.31.194

location.host // 192.168.31.194:8000

location.port // 8000

location.protocol // http:

location.search // ?a=3&c=aa

location.hash // #b

location.pathname // /jquery/index.html

 

全域對象

資訊彈出框

alert(‘你很帥!‘)

 

確認彈出框

var isReady = confirm(‘準備好來嗎?‘)

if (isReady) {

 

} else {

 

}

 

輸入資訊彈出框

var name = prompt(‘請輸入你的名字‘)

console.log(name)

 

開啟一個新視窗

open(‘http://baidu.com‘)

// 改變某個視窗的 url

open(‘http://baidu.com‘, ‘a‘)

open(‘http://youku.com‘, ‘a‘) // 上面視窗的地址會從 百度 變成 優酷的

 

過段時間後執行

// 1 秒後執行

setTimeout(function() {

  // doSth

}, 1000)

 

每隔一段時間執行

var i = 1

// 每隔 1 秒後執行

var runId = setInterval(function(){

  console.log(i++)

  if(i >= 10) {

    clearInterval(runId) // 停止執行

  }

}, 1000)

 

螢幕每次重新整理時執行

一般是渲染一些 UI。

 

var targetEl = document.querySelector(‘.tar‘)

var res

var isStop

var runId = requestAnimationFrame(function(){

  targetEl.text(res)

  if(isStop) {

    cancelAnimationFrame(runId)

  }

})

 

在控制台輸出資訊

console.log(‘日誌資訊‘)

console.warn(‘警告資訊‘)

console.info(‘普通訊息‘)

console.error(‘錯誤資訊‘)

 

console.log(‘1+2 = %d‘, 3)

var person = {name: ‘Joel‘, gender: ‘男‘}

console.log(‘名字:%s,性別:%s‘, person.name, person.gender)

161122、BOM 操作寫法樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.