实用javascript函数锦集一
<div>
<div> </div>
<div>2、获取cookie的键值:</div>
<div>
<blockquote>function cookie(name) {<br />
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');<br />
var r = document.cookie.match(reg);<br />
if (r != null) {<br />
return unescape(r);<br />
}<br />
return null;<br />
}</blockquote>
</div>
<div>其实这个和例1非常相似,只是改了一个匹配对象,用法完全一样。</div>
<div> </div>
<div>
<p>1、JS获取URL中的参数值:</p>
<table border="1" cellpadding="0" cellspacing="0" class="php syntaxhighlighter">
<tbody>
<tr>
<td style="border-color:initial !important; vertical-align:baseline !important; width:725px">
<div>
<div> //根据参数名从url中获取对应参数值</div>
<div> function getQueryString(name) {</div>
<div> var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');</div>
<div> var r = window.location.search.substr(1).match(reg);</div>
<div> if (r != null) {</div>
<div> return unescape(r);</div>
<div> }</div>
<div> return null;</div>
<div> }</div>
</div>
</td>
</tr>
</tbody>
</table>
<p>使用实例:</p>
<p>http://www.runoob.com/index.php?id=1&image=awesome.jpg</p>
<p>调用 getQueryString("id") 返回 1。</p>
<p>调用 getQueryString("image") 返回 "awesome.jpg"。<br />
<br />
另外ckeditor示例的一个方法:</p>
<div style="background:#eeeeee; border:1px solid #cccccc; padding:5px 10px">
<pre>
// Helper function to get parameters from the query string.
function getUrlParam( paramName ) {
var reParam = new RegExp( '(?:[\?&]|&)' + paramName + '=([^&]+)', 'i' );
var match = window.location.search.match( reParam );
return ( match && match.length > 1 ) ? match : null;
}</pre>
</div>
</div>
</div>
页:
[1]