js实现html 页面之间的跳转传参

一、页面之间的跳转传参

1、在页面之间跳转的方式有两种:

window.location.href=”test.html?num=10” 地址会改变参数也会被传递但是不会打开新窗口

window.open("test.html") 这样会重新打开一个新窗口。

2、获取参数

如果是按照第一种方式进行了传递则有参数,那么我们怎们获取url中的参数那,那就使用js默认的属性: var url = location.search;

其中的location.search 就是js自动获取url中? 后的所有值。获取了这个之后就可以使用substring,split等来获取参数了。

3、实例展示

 // 跳转url 以及传递的参数
	window.location.href='http://img.as.com/news/image/newscenter/20111107zt/whd/30share/jieguo1n.html?money='+nums+'&url='+fxurl;
 
        // 获取money,以及分型的地址
    function GetRequest() {
          var url = location.search; 
         var theRequest = new Object();
          if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            //alert(str);
            var strs= new Array();   
             strs = str.split('&');
            var money=strs[0].substring(6);
            fxurl=(strs[1].substring(4)).trim();
            //alert(fxurl);
            var  view=money+"元";
            $("#jieguo1m").html(view);
      }
}
GetRequest();

  

这样当跳转到url指定的页面后,调用GetRequest();这个函数,函数中的location.search;来获取了url中?后的所有参数,接下来就是按照需求来解析了。

 

转载于:https://blog.csdn.net/QH_JAVA/article/details/50381231



posted on 2019-05-30 11:22  求职若渴,谦言气盛  阅读(850)  评论(0编辑  收藏  举报