


Detailed explanation of the front-end method of passing parameters between html pages
A situation that often occurs in projects is that there is a list, such as a case list. Click on an item in the list to jump to the details page. The details are generated based on a clicked record, because the cases and specific details pages are added by users later. When we start writing, it is impossible to exhaust them all. Therefore, when jumping to the page, we need to pass a parameter so that we can make a data request through this parameter, and then generate the page based on the data returned by the background. Therefore, jumping through the a tag will definitely not work.
We often write form forms. When submitting, we can pass parameters. If we use the form and hide it, the effect should be achieved.
In addition, window.location.href and window.open can also achieve the effect.
1. Pass parameters through the form form
<html> <head> <!--网站编码格式,UTF-8 国际编码,GBK或 gb2312 中文编码--> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <meta name="Keywords" content="关键词一,关键词二"> <meta name="Description" content="网站描述内容"> <meta name="Author" content="Yvette Lau"> <title>Document</title> <!--css js 文件的引入--> <!-- <link rel="shortcut icon" href="images/favicon.ico"> --> <link rel="stylesheet" href=""/> <script type = "text/javascript" src = "jquery-1.11.2.min.js"></script> </head> <body> <form name = "frm" method = "get" action = "receive.html" onsubmit = "return foo()" style = "position:relative;"> <input type="hidden" name="hid" value = "" index = "lemon"> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn//upload/image/325/277/595/1531357491512805.png?x-oss-process=image/resize,p_40" class="lazy" src = "main_jpg10.png" / alt="Detailed explanation of the front-end method of passing parameters between html pages" > <input type = "submit" style = "position:absolute;left:10px;top:0px;width:120px;height:40px;opacity:0;cursor:pointer;"/> </form> <form name = "frm" method = "get" action = "receive.html" onsubmit = "return foo()" style = "position:relative;"> <input type="hidden" name="hid" value = "" index = "aaa"> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn//upload/image/325/277/595/1531357491512805.png?x-oss-process=image/resize,p_40" class="lazy" src = "main_jpg10.png" / alt="Detailed explanation of the front-end method of passing parameters between html pages" > <input type = "submit" style = "position:absolute;left:10px;top:0px;width:120px;height:40px;opacity:0;cursor:pointer;"/> </form> <form name = "frm" method = "get" action = "receive.html" onsubmit = "return foo()" style = "position:relative;"> <input type="hidden" name="hid" value = "" index = "bbb"> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn//upload/image/325/277/595/1531357491512805.png?x-oss-process=image/resize,p_40" class="lazy" src = "main_jpg10.png" / alt="Detailed explanation of the front-end method of passing parameters between html pages" > <input type = "submit" style = "position:absolute;left:10px;top:0px;width:120px;height:40px;opacity:0;cursor:pointer;"/> </form> </body> </html> <script> function foo(){ var frm = window.event.srcElement; frm.hid.value = $(frm.hid).attr("index"); return true; } </script>
When you click on the picture, jump to the receive.html page. The url of the page becomes:
#The string we want to pass has been passed.
Then perform string splitting on the current url
window.location.href.split(“=”)[1]//Get lemon
After we get the parameters that need to be passed, we can proceed to the next step based on this.
In addition to the above-mentioned string splitting to obtain the parameters passed by the url, we can also obtain them through regular matching and the window.location.search method.
2. Through window.location.href
For example, when we click on a list, we need to pass a string to the detail.html page, and then the detail.html page passes Ajax interactive data, loading the content of the page.
var index = "lemon"; var url = "receive.html?index="+index; $("#more").click(function(){ window.location.href = url; });
The current page will be replaced with the recieve.html page, and the url of the page becomes:
Then we use The above method extracts the parameters you need
3. Through window.location.open
If you want to open a new page instead of changing the current page, then window.location.href Not applicable. At this time, we need to use window.location.open() to achieve
A brief introduction to the window.open() function. window.open() has three parameters. The first parameter is the url of the page to be opened, the second parameter is the window target, the third parameter is a specific string and a Boolean value indicating whether the new page replaces the currently loaded page in the browser history, by passing only the first parameter. The second parameter can also be a special window name such as "_blank", "_self", "_parent", "_top". "_blank" opens a new window, and "_self" achieves the same effect as window.location.href.
Continue the above example:
<script> var index = "lemon"; var url = "receive.html?index="+index; $("#more").click(function(){ window.open(url) }); </script>
In this way, when clicked, a new page will be opened, and the url address of the page is the same as above.
Due to browser security restrictions, some browsers have added restrictions on pop-up window configuration. Most browsers have built-in pop-up window blocking programs. Therefore, pop-up windows may be blocked. When the pop-up window is When blocking, you need to consider two possibilities. One is that the browser's built-in blocking program blocks pop-up windows. Then window.open() is likely to return Null. At this time, you can determine whether the pop-up window is blocked by monitoring the returned value. shield.
var newWin = window.open(url); if(newWin == null){ alert("弹窗被阻止"); }
The other is a pop-up window blocked by a browser extension or other program, then window.open() usually throws an error. Therefore, to accurately detect whether the pop-up window is blocked, it must be While detecting the return value, encapsulate window.open() in a try-catch block. In the above example, it can be written in the following form:
<script> var blocked = false; try{ var index = "lemon"; var url = "receive.html?index="+index; $("#more").click(function(){ var newWin = window.open(url); if(newWin == null){ blocked = true; } }); } catch(ex){ block = true; } if(blocked){ alert("弹出窗口被阻止"); } </script>
The above is the detailed content of Detailed explanation of the front-end method of passing parameters between html pages. For more information, please follow other related articles on the PHP Chinese website!

TheroottaginanHTMLdocumentis.Itservesasthetop-levelelementthatencapsulatesallothercontent,ensuringproperdocumentstructureandbrowserparsing.

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for

The article explains the HTML 'class' attribute's role in grouping elements for styling and JavaScript manipulation, contrasting it with the unique 'id' attribute.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
