js获取地址栏参数document.getElementById("").innerHTML

JavaScript016

js获取地址栏参数document.getElementById("").innerHTML,第1张

可以用正则表达式实现。

window.location.search

是你的查询字符串

"?id=123"

用下面的正则表达式提取出查询字符串的值

/[\?&]id=([^&=]*)/i

所以就有下面的代码:

var matches = /[\?&]id=([^&=]*)/i.exec(window.location.search), id

if(!matches) {

  // 参数不正确:没有指定 id,可以在这里做一些其他的处理

}

else {

  id = decodeURIComponent(matches[1])

  // 这就拿到 ID 了

}

第一种,判断js对象中是否有某个属性

var obj = {test : 'test'}

if('test' in obj){

    console.log('yes')

} else {

    console.log('no')

}

第二种,判断js对象本身是否有某个属性(所谓本身有意思是,必须属性是直接在对象上的,而不是通过原型链上找到的。

var Base = function(){}

Base.prototype.test = 'test'

var obj = new Base()

obj.test2 = 'test2'

if('test1' in obj){

    console.log('yes')

} else {

    console.log('no')

}

if(obj.hasOwnProperty('test2')){

    console.log('own')

} else {

    console.log('none')

}

//用in 操作符,可以判断有没有。 用hasOwnProperty来判断在自身有没有。