search
HomeDatabaseMysql TutorialWeb实用技巧总结

Web实用技巧总结

Jun 07, 2016 pm 03:36 PM
webhowPractical TipsSummarizeaskpage

1、如何每次请求Web页面都取最新版本,而不是浏览器缓存中的页面 l 问题:浏览器中可以设定缓存选项来设置是否使用页面缓存,所以没法强制用户设定所有的Web页面都不使用缓存。 l 解决方法:在页面的HEAD标记中添加下面的标记,以保证该页面不缓存,每次请求

1、如何每次请求Web页面都取最新版本,而不是浏览器缓存中的页面

l  问题:浏览器中可以设定缓存选项来设置是否使用页面缓存,所以没法强制用户设定所有的Web页面都不使用缓存。

l  解决方法:在页面的

标记中添加下面的标记,以保证该页面不缓存,每次请求都取最新版本。

  

l  浏览器缓存设置是针对所有页面的,而这种设置方法是针对特定单个页面的,会覆盖浏览器缓存设置。

2、在使用window.showModalDialog()方法打开窗口中,如何提交表单不会弹出新窗口?

l  问题:首先window.showModalDialog()只在IE浏览器中有效。在使用window.showModalDialog()方法打开窗口中提交表单时,IE浏览器默认情况在新窗口中显示结果页面。

l  解决方法:在页面的

标记中添加下面的标记,指定基本目标窗口为_self。这样,提交表单时,就会在当前窗口中显示结果页面。

 

3、双表头固定的数据列表中,滚动条同步移动的实现

l  问题:Web中单表头固定的数据列表使用比较多,其滚动条移动的实现很简单,只要使用

标记,设置其的样式就可以了。但有时也需要使用双表头固定的数据列表,典型的例子就是人员的日程安排。

l  解决方法:分别用三个

标记包含上表头(topheader),左表头(leftheader)和数据内容(content),只有content有滚动条;当移动滚动条时(产生onscroll事件),设置topheader的scrollLeft属性以及leftheader 的scrollTop属性,使其和content保持一致。

l  下面是一个实现例子的核心代码片断,做一下简单说明,源代码可以在这里下载。

function hasAttribute(object, attrName) {

if (typeof object !== "object" && typeof object !== "string") {

return false;

}

return attrName in object;

}

function syncScroll(topId, leftId, contentId) {

var topHeader = document.getElementById(topId);

var leftHeader = document.getElementById(leftId);

var content = document.getElementById(contentId);

if (topHeader && hasAttribute(topHeader, 'scrollLeft') && content && hasAttribute(content, 'scrollLeft')) {

topHeader.scrollLeft=content.scrollLeft;

}

if (leftHeader && hasAttribute(leftHeader, 'scrollTop') && content && hasAttribute(content, 'scrollTop')) {

leftHeader.scrollTop=content.scrollTop;

}

}

l  先来看三个

标记的样式:

?  overflow: scroll表示当

标记中的内容溢出时可以滚动

?  topheader和leftheader的overflow-x及overflow-y属性设置为hidden,表示溢出时不显示滚动条,而content设置为auto

?  content的width值比topheader的大16px,而height值比leftheader的大17px;这是为滚动条预留的(滚动条大概16-17px,根据具体   情况微调)

?  word-break: break-all用来保证数据过长时自动换行,以免将单元格宽度撑大而错位

l  topheader和content内部包含的

的width设置为绝对大小,并大于对应
的width,以便出现水平滚动条

syncScroll()函数在onscroll事件触发时调用,使topheader的scrollLeft属性以及leftheader 的scrollTop属性和content的保持同步

l  上面的例子个静态页面,在实际应用中通常是动态页面,需要注意以下一些方面:

?  显示的列数不定时(例如每个月的天数有所不同),需要动态计算

的width值

?  当content内部包含的

的width值

?  同样,当content内部包含的

的height值

?  word-break: break-all保证了单元格宽度不会撑大;但当单元格内容显示不下时,会自动撑大宽度,所以当表格的行距不同时,要动态计算以决定是否要出垂直滚动条,这个要麻烦一些

4、全角半角字符混合输入的处理

l  问题1:输入长度的验证,例如输入内容在数据库中是40字节,所以输入长度不能超过40字节;而JavaScript中的String.length获得的是字符个数。

l  解决方法:通常全角字符为2字节,而半角字符为1字节;这样String.length获得的长度相当于将全角字符作为1字节处理,所以再加上全角字符的个数就是字节数。考虑到escape()函数处理的字符串中,全角字符都转换成%uXXXX的Unicode形式,可以由此统计出字符串中的全角字符个数。

l  下面的JavaScript代码扩展了String对象,用来获取字符串的字节数

String.prototype.getBytesLength = function () {

return this.length + (escape(this).split("%u").length - 1);

};

l  问题2:由于显示空间有限,要求显示内容按指定长度(字节数)截取,这里有全角字符的前一个字节在指定长度范围里的问题。

l  解决方法:从字符串后端开始,逐个删除字符,直到字符串字节数

l  下面的JavaScript代码扩展了String对象,实现了上面的截取方法

String.prototype.removeCharAt = function (index) {

var retStr = this;

if (index

if (index === 0) {

retStr = this.substring(1);

} else {

if (index == this.length - 1) {

retStr = this.substring(0, this.length - 1);

} else {

retStr = this.substring(0, index) + this.substring(index + 1);

}

}

}

return retStr;

};

String.prototype.intercept = function (length) {

var s = this;

var len = s.getBytesLength();

while (len > length) {

s = s.removeCharAt(s.length - 1);

len = s.getBytesLength();

}

return s;

};

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment