wps怎么用js获取链接地址

JavaScript014

wps怎么用js获取链接地址,第1张

可以使用 JavaScript 的 window.location 对象来获取链接地址,该对象包括以下属性:

- hash:返回 URL 中的哈希部分(以 # 开头)

- host:返回 URL 中的主机名和端口号

- hostname:返回 URL 中的主机名

- href:返回整个 URL(包括协议名,域名,端口号,路径,查询字符串等)

- pathname:返回 URL 中的路径

- port:返回 URL 中的端口号

- protocol:返回 URL 中的协议名

- search:返回 URL 中的查询字符串(以 ? 开头)

可以用getAttribute()方法获取。

注:总结了一些getAttribute(),setAttribute()在不同浏览器下兼容性以及如何解决这些问题

body里面有这些内容:

<div id="idHeader" class="class-header" title="kingwell" status="1"></div>

<label id="forUserName" for="userName" title="kingwell" status="1"></label>

下面是script的测试:

var el = document.getElementById("idHeader")

alert(el.getAttribute("id"))

alert(el.id)

IE Firfox->idHeader

alert(el.getAttribute("class"))

//IE6,IE7 ->null IE8,IE9,Firefox ->class-header

alert(el.class)

//IE6,IE7,IE8->报错 IE9,Firefox->undefined

alert(el.getAttribute("className"))

//IE6,IE7->class-header IE8,IE9,Firefox ->undefined

alert(el.className)

//All ->class-header

var elfor = document.getElementById("forUserName")

alert(elfor.getAttribute("for"))

//IE6,IE7->undefined IE8,9,Firefox->forUseName

alert(elfor.for )

//IE6,IE7报错,其它为undefined

alert(elfor.title)

//全部输出kingwell

alert(elfor.status)

//IE6-8 ->1 IE9,Firefox->undefined

alert(elfor.getAttribute("status"))

//全部输出 1

总结:

1:常规属性建议使用 node.XXXX。

2:自定义属性建议使用node.getAttribute("XXXX")。

3:当获取的目标是JS里的关键字时建议使用node.getAttribute("XXX"),如label中的for。

4:当获取的目标是保留字,如:class,请使用className代替。

的确,你的问题说的好含糊,我估计你自己都不知道怎么问吧

我觉得你想要表达的是:1获得参数prodname,2你要把获得的那些js代码搬到一个外部的js文件中

如果真是这样的话,再考虑看下面的内容。

1,你先不用在外部js文件中写js代码,就在本页写好,最后再移出去。

2,我在网上找了个查找参数的方法 //获得地址栏参数

function GetQueryString(sProp)

{

var re = new RegExp("[&,?]"+sProp + "=([^\\&]*)", "i")

var a = re.exec(document.location.search)

if (a == null)

return ""

return a[1]

},sProp就是你参数名。