我需要幫助從URL使用JavaScript獲取傳遞參數的值。 在我的索引HTML我定義如下:需要幫助獲取通過URL從URL傳遞的參數值
<body bgcolor="#ffa800" background="file:///c:/website/images/background.jpg">
<script src="jquery-1.10.2.min.js"></script>
<script>
alert("Initializing the Cart");
var params = { cart1:1 };
var cart1 = jQuery.param(params);
alert(cart1);
</script>
然後我嘗試從我的索引HTML調用另一個網頁:
我的警報顯示cart1 = 1
在其他網頁vision.html我有:
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Vision</title>
<script src="jquery-1.10.2.min.js"></script>
<script>
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function checkcart()
{
alert("Checking cart");
var cartstatus = getUrlVars()["cartx"];
alert(cartstatus);
}
</script>
</head>
<body onload="checkcart()" bgcolor="#F0FFF0">
我對cartstatus的警報顯示:cart1 我不知道爲什麼沒有給出cart1的價值。提前致謝。你通過了,當你從數組把它作爲重點
file:///C:/Users/fs/Desktop/1.htm?cartx=3
您的變量的