js 原生开发:实现页面带参跳转:从传参到接参,封装方法,实现一行代码精准接参(可传对象)iEieie

news/2024/7/19 13:05:06 标签: html, javascript, js

目前知道两种方法

1.通过地址栏的跳转链接带参
在这里插入图片描述

index.html

html"><!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title></title>
	</head>
	<body>
		<button type="button" id="btn">点击跳转</button>
		
		<script type="text/javascript">javascript">
			document.getElementById("btn").onclick= function  () {
				let param  = {
					name:"张三",
					age:"18",
					miaoshu:"张三的传奇一生"
					
				}
				window.location.href = "new_file.html?name="+param.age
			}
		</script>
	</body>
</html>


new_file.html

html"><!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title></title>
	</head>
	<body>
		<h1 style="text-align: center;">新页面</h1>
		<script type="text/javascript">javascript">
			
			var info = location.search.slice(); // 从location对象中的search属性得到地址栏里 ? 后面的参数       【接参】
			console.log(info)
		</script>
	</body>
</html>

new_file.html跳转之后的效果是这样的

在这里插入图片描述

运行结果显示拿到了路径后面的参数,但是没有拿到自己具体想要传递的参数,下面封装了一个很实用的方法,用于拿到想要的参数:

javascript">var parseQueryString = function () {
			
				var str = window.location.search;
				var objURL = {};
			
				str.replace(
					new RegExp("([^?=&]+)(=([^&]*))?", "g"),
					function ($0, $1, $2, $3) {
						objURL[$1] = $3;
					}
				);
				return objURL;
			};
			
			//Example how to use it: 
			var params = parseQueryString();
			alert(params["name"]); 

所以new_file.html 文件最终代码是:

html"><!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title></title>
	</head>
	<body>
		<h1 style="text-align: center;">新页面</h1>
		<script type="text/javascript">javascript">
			
			var parseQueryString = function () {
			
				var str = window.location.search;
				var objURL = {};
			
				str.replace(
					new RegExp("([^?=&]+)(=([^&]*))?", "g"),
					function ($0, $1, $2, $3) {
						objURL[$1] = $3;
					}
				);
				return objURL;
			};
			
			//Example how to use it: 
			var params = parseQueryString();
			
			alert(params["name"]); 
			
			
		</script>
	</body>
</html>

注:谷歌会对路径的参数进行转码,所以在谷歌用此方法传递参数时可以传递阿拉伯数字,不能传递中文参数,在IE则不存在该问题

2.通过localStorage传参

在这里插入图片描述

index.html

html"><!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>准备跳过去的页面</title>
	<style>
		body{
			text-align: center;
		}
	</style>
</head>
<body>
	<h1>我是准备<a href="./new_file.html">跳转</a>的页面</h1>
	
	<script>javascript">
		// 定义要传的参数
		const param = {
			name:'全易',
			age:'23',
			sex:'girl',
			position:'font-end developer',
			blogurl:'https://blog.csdn.net/qq_42618566'
		}
		/* //如果是多条数据加上这个数组JSON.stringify(param)改为JSON.stringify(array)
		var array = [];
		array.push(param); */
		const params = JSON.stringify(param); // 因为localStorage只能存字符串,所以需要转化成JSON 字符串。
		localStorage.setItem('params',params);
	</script>
</body>
</html>

new_file.html

html"><!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>跳过来的页面</title>
		<style>
			body{
				text-align: center;
			}
		</style>
	</head>
	<body>
		<h1>我是跳转过来的页面</h1>
		<h4>取的参数如下:</h4>
		<div style="min-width: 480px; max-width: 36%;margin: 0 auto; text-align: left;">
			<h4>姓名:<span class="name"></span></h4>
			<h4>性别:<span class="sex"></span></h4>
			<h4>职业:<span class="position"></span></h4>
			<h4>csdn:<a class="link" href=""></a></h4>
		</div>
		
		<script>javascript">
			// 取参并转为json对象
			const param = JSON.parse(localStorage.params);
			// 获取页面元素并赋值
			document.querySelector('.name').innerText = param.name;
			document.querySelector('.sex').innerText = param.sex;
			document.querySelector('.position').innerText = param.position;
			document.querySelector('.link').innerText = param.blogurl;
			document.querySelector('.link').href = param.blogurl;
		</script>
	</body>
</html>

该方式的原理就是两个页面之间有个中转站,就相当于快递员(存数据的页面)把快递(数据)放到了快递站(localStorage),然后你(取数据的页面)去快递站(localStorage)去取。
效果如下:
在这里插入图片描述


http://www.niftyadmin.cn/n/936629.html

相关文章

Visual C#两分钟搭建BHO IE钩子

微软在1997年正式推出Browser Helper Object (BHO), 使程序员能够更好的对IE进行二次开发和操作. 在通过编写BHO程序数月后, 我希望把我的一些经验告诉才开始的同志, 避免走一些弯路. 我本人是非常喜欢C的. 因为C对内存直接操作的方式可以节省非常多的内存损耗, 也更快一些. 但…

最大的Redis集群:新浪Redis集群揭秘

前言 Tape is Dead&#xff0c;Disk is Tape&#xff0c;Flash is Disk&#xff0c;RAM Locality is King. — Jim Gray Redis不是比较成熟的Memcache或者Mysql的替代品&#xff0c;是对于大型互联网类应用在架构上很好的补充。现在有越来越多的应用也在纷纷基于Redis做架…

SUSE上配置SAMBA服务

在*nix上安装samba的方法有很多&#xff0c;debian系的apt和.deb安装&#xff0c;redhat系的yum和.rpm包安装&#xff0c;还有通用的ios挂在安装和源代码编译安装&#xff0c;我介绍的是使用下载的源代码安装。原因有两点&#xff0c;其一是源代码安装是通用的方法&#xff0c;…

处理不平衡数据:技术详解与实例分析

在机器学习和数据科学的世界里&#xff0c;不平衡数据集是一个常见的问题。不平衡数据集指的是在分类问题中&#xff0c;目标变量的类别分布不均&#xff0c;即某一类别的样本数量远超过其他类别。本文将详细讲解如何处理不平衡数据&#xff0c;包括重采样方法、集成方法和适用…

VUE2 2.9.6 无法卸载问题的解决办法

1、删除硬盘中的vue cli 使用 where vue 查看 vue的安装位置 使用vue -V查看vue是否被卸载 2、安装新版vue 使用命令npm install -g vue/cli 查看vue版本

Core Data 版本数据迁移

Core Data版本迁移基础 通常&#xff0c;在使用Core Data的iOS App上&#xff0c;不同版本上的数据模型变更引发的数据迁移都是由Core Data来负责完成的。这种数据迁移模式称为Lightweight Migration&#xff08;可能对于开发人员来说是lightweight&#xff09;&#xff0c;开发…

vue3 安装和配置vue全家桶

一、Vue-CLI3新建项目 1.新建项目 vue create project-name 二、配置全家桶 2.安装路由 npm install vue-router --save-dev router > index.js 配置代码 import Vue from vue import VueRouter from vue-router //导入路由 // 解决ElementUI导航栏中的vue-router在3…

out与ref修饰符

out修饰符 定义 作用 使用注意 总结 定义 out意为output,所以被out修饰的参数叫做输出参数. 通过使用out修饰的参数,方法可以返回对应参数的值 作用 先看一个例子定义变量:1 int x1,y2,sum;//定义x,y,sum三个整型变量 定义方法:1 static void Add(int x,int y,out int sum) 2 {…