2014-03-25 83 views
1

我是JavaScript新手,我試圖從我創建的Web上的查詢字符串中提取信息,如果我直接在頁面上加載而沒有查詢字符串,頁面將加載,但一旦我從我的表單頁面重定向到我正在執行解析的頁面凍結和崩潰...任何人都可以幫忙! :(試圖從查詢字符串中提取信息

http://main.xfiddle.com/7d679c3a/Project1/Commission.php 
http://main.xfiddle.com/7d679c3a/Project1/contactForm.php 
http://main.xfiddle.com/7d679c3a/Project1/DiceRoll.php 
http://main.xfiddle.com/7d679c3a/Project1/IsEven.php 
http://main.xfiddle.com/7d679c3a/Project1/palindrome.php 
http://main.xfiddle.com/7d679c3a/Project1/part1.php 
http://main.xfiddle.com/7d679c3a/Project1/passwordStrength.php 
http://main.xfiddle.com/7d679c3a/Project1/allinOne.php 

JavaScript代碼

var $ = function(id) 
{ 
return document.getElementById(id); 
} 

var formInfo = location.search(); 
    formInfo = formInfo.substring(1, formInfo.length); 

while (formInfo.indexOf("+") != -1) 
{ 
    formInfo = formInfo.replace("+", " "); 
} 



while (formInfo.indexOf("=") != -1) 
{ 
    formInfo.replace("=", " "); 
} 

formInfo = decodeURI(formInfo); 
formInfo.replace("firstname", ""); 
formInfo.replace("lastname", ""); 
formInfo.replace("phonenumber", "");  
formInfo.replace("postalcode", ""); 
formInfo.replace("startingmoney", ""); 

var infoArray = formInfo.split("&"); 

var firstName = infoArray[0]; 
var lastName = infoArray[1]; 
var phoneNumber = infoArray[2]; 
var postalCode = infoArray[3]; 
var startingMoney = infoArray[4]; 

$("playername").innerHTML = firstName + " " +lastName; 
$("playerinfo").innerHTML = phoneNumber + " " + postalCode; 
$("money").innerHTML = " $$" + startingMoney; 

HTML代碼

<div id="fireinfo"> 
<p id="playername"></p><br/> 
<p id="playerinfo"></p> 
<p id="money"></p> 
</div> 

我想出來把我從查詢字符串獲取到播放器名稱的信息,播放器info and money id's。

回答

0

使用此函數獲取查詢字符串值:

function getQueryString(name) { 
     name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
     var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), 
      results = regex.exec(window.location.search); 

     return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); 
    }; 

    var firstname = getQueryString('firstname');