This article brings you a summary of the methods of passing parameters in js (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Implicitly create html tags
<input>
This method is generally used with ajax. The above value uses the template engine
2.window['data']
window['name'] = "the window object";
3. Use localStorage, cookies, etc. to store
window.localStorage.setItem("name", "xiaoyueyue"); window.localStorage.getItem("name")
Features
1、localStorage 是持久存储,不主动删除 一直存在 sessionStorage 是临时存储,关闭浏览器数据就没了 2、localStorage 可以多窗口共享 sessionStorage 不能多窗口共享数据
4. Get the address bar method
My own encapsulated method
function parseParam(url) { var paramArr = decodeURI(url).split("?")[1].split("&"), obj = {}; for (var i = 0; i <p>2. Regular expression method</p><pre class="brush:php;toolbar:false">function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }
5. Label binding function parameter passing
<!--base--> <button>test1</button> <!--高级--> <button>test</button>
this extension
Use This parameter passing was figured out while using art-template. It is no longer necessary to pass only one ID and splice it into several parameters! happy!
var box = document.createElement("p"); box.innerHTML = "<button>点击</button>"; document.body.appendChild(box); // name,age,friend function alertInfo(val) { console.log(val); alert('大家好,我是' + val.dataset.name + ', 我今年' + val.dataset.age + '岁了,我的好朋友是' + val.dataset.friend + ' !') }
event
Since this can be used, the event.target
method is also available in the event:
Get the current index value according to class , the parameters can be event objects
var getIndexByClass = function (param) { var element = param.classname ? param : param.target; var className = element.classname; var domArr = Array.prototype.slice.call(document.querySelectorAll('.' + className)); for (var index = 0; index <h2 id="HTML-data-Custom-attributes">6.HTML5 data-* Custom attributes</h2><pre class="brush:php;toolbar:false"><button>点击</button>
var btn = document.querySelector("button") btn.onclick = function () { alert(this.dataset.name) }
7. String parameters
Single parameter
var name = 'xiaoyueyue', age = 25; var box = document.createElement("p"); box.innerHTML = '<button>点击</button>'; console.log(box) document.body.appendChild(box); // name, age function alertInfo(name, age, home, friend) { alert("我是" + name) }
Multi-parameter passing
var name = 'xiaoyueyue', age = '25', home = 'shanxi', friend = 'heizi'; var params = """ + name + "","" + age + "","" + home + "","" + friend + """; var box = document.createElement("p"); box.innerHTML = "<button>点击</button>"; console.log(box) document.body.appendChild(box); // name, age,home,friend function alertInfo(name, age, home, friend) { alert("我是" + name + ',' + "我今年" + age + "岁了!") }
Complex parameter passing
var data = [ { "name": "xiaoyueyue", "age": "25", "home": "shanxi", "friend": "heizi" } ] var box = document.createElement("p"); for (var i = 0; i 点击"; } document.body.appendChild(box); function alertInfo(id, name, age, home, friend) { alert("我是" + name + "," + friend + "是我的好朋友") }
8.arguments
arguments
Objects are local variables available in all (non-arrow) functions . You can use the arguments object to reference a function's arguments within a function. It is an array-like object.
<button>分配</button>
function fenpei() { var args = Array.prototype.slice.call(arguments); alert("我是" + args[2] + "油品,数量为 " + args[1] + " 吨, id为 " + args[0]) }
Related recommendations:
js anonymous function parameter passing method
JS parameters passed to php are garbled
How to use js to pass parameters to php files
The above is the detailed content of Summary of methods for passing parameters in js (with code). For more information, please follow other related articles on the PHP Chinese website!

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools