JavaScript/js 傳輸與接收參數的執行個體解析

來源:互聯網
上載者:User

在HTML中用JS接收參數

 

先介紹一下JS中處理URL的方法:
網址樣本:http://localhost/test/test.htm?id=1

<script languge=javascript>
alert(window.location.pathname); --返回 /test/test.htm
alert(window.location.search); --返回 ?id=1
alert(window.location.href); --返回 http://localhost/test/test.htm?id=1
</script>

---------------------------------
location對象
含有當前URL的資訊.
屬性
href 整個URL字串.
protocol 含有URL第一部分的字串,如http:
host 包含有URL中主機名稱:連接埠號碼部分的字串.如//www.100-tea.com/zixun/
hostname 包含URL中主機名稱的字串.如http://www.100-tea.com/ ;
port 包含URL中可能存在的連接埠號碼字串.
pathname URL中"/"以後的部分.如~list/index.htm
hash "#"號(CGI參數)之後的字串.
search "?"號(CGI參數)之後的字串.

在HTML中用JS接收參數用到的函數
function getParameter(param)
{
var query = window.location.search;
var iLen = param.length;
var iStart = query.indexOf(param);
if (iStart == -1)
return "";
iStart += iLen + 1;
var iEnd = query.indexOf("&", iStart);
if (iEnd == -1)
return query.substring(iStart);
return query.substring(iStart, iEnd);
}
使用的時候:var temp = getParameter("傳過來的參數");
必須得xx.html?xx=xx這樣的傳參形式。。。。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.