正文
如何从页面1中向页面2传入一张图片的路径呢?
首先来复习下window.location
window.location对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面.
<!--设置或获取对象指定的文件名或路径。
console.log(window.location.pathname)
设置或获取整个 URL 为字符串。
console.log(window.location.href);
设置或获取与 URL 关联的端口号码。
console.log(window.location.port)
设置或获取 URL 的协议部分。
console.log(window.location.protocol)
设置或获取 href 属性中在井号“#”后面的分段。
console.log(window.location.hash)
设置或获取 location 或 URL 的 hostname 和 port 号码。
console.log(window.location.host)
设置或获取 href 属性中跟在问号后面的部分。
console.log(window.location.search)-->
因此这儿我们用到window.location.search
1 . 在页面1,通过点击,跳转到页面2,
//$('#aa').click()即为给id名为aa的元素添加点击事件
$('#aa').click(function(){
window.location.href="index3.html/?src=img/apple.jpg";
})
2 . 这样页面2的url为下图所示
3 .在页面2中通过对url进行操作。
//创建变量sear来接受window.location.search的值
var sear=window.location.search;//即为"?src=img/apple.jpg"
console.log(sear)
//判断是否存在sear
if(sear.indexOf('?')!=-) arr=sear.substr().split('&');//arr=["src=img/apple.jpg"]
new_arr=arr[].split('=');//new_arr=["img/apple.jpg"]
console.log(arr);
}
4.这样就可以将页面1中的参数传到页面2啦