2010-08-20 139 views
2

我正在使用以下代碼打開一個彈出窗口並傳遞查詢字符串的ID。使用Request.QueryString()獲取變量值時出現未定義錯誤

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<script language="javascript" type="text/javascript"> 
function openwindow(divID) { 
      window.open("pp.html?id="+divID+"","","status=yes, location=yes, width=700, height=400"); 
    } 
</script> 
</head> 
<body> 
<a href="#" onclick="openwindow('one')" id="one">One</a> 
<br /> 
<a href="#" onclick="openwindow('two')" id="two">Two</a> 
<br /> 
<a href="#" onclick="openwindow('three')" id="three">Three</a> 
</body> 
</html> 

這個場景是,我需要在彈出窗口中顯示其ID與查詢字符串值類似的DIV。彈出窗口代碼是

<html> 
<head> 
<script language="javascript" type="text/javascript"> 
function getid() { 
    if (Request.QueryString("id")!=null) 
     var id = Request.QueryString("id"); 
     document.getElementById(id).style.display = "block"; 
} 
</script> 
</head> 
<body onload="getid();"> 
<div style=" overflow:hidden"> 
<div style="margin-left:-5px;"><input type="file" style="" /></div> 
</div> 
<div style="width:200px; height:200px; border:1px solid #999999; background- color:#CCCCCC; display:none" id="one">Hello! ONE</div> 
<div style="width:200px; height:200px; border:1px solid #999999; background-color:#CCCCCC; display:none" id="two">Hello! TWO</div> 
<div style="width:200px; height:200px; border:1px solid #999999; background-color:#CCCCCC; display:none" id="three">Hello! THREE</div> 
</body> 
</html> 

現在,彈出式窗口出現錯誤「請求未定義」。

請幫我解決方案。

感謝 LOKESH亞達夫

回答

1

你混合使用JavaScript這當然是不可能的ASP.NET服務器端編程語言。試試這樣:

function getid() { 
    <% if (Request.QueryString("id") != null) { %> 
     var id = '<%= Request.QueryString("id") %>'; 
     document.getElementById(id).style.display = 'block'; 
    <% } %> 
} 

如果你不使用你可以使用下面的函數讀取JavaScript的查詢字符串參數的服務器端語言(taken from here):

function gup(name) 
{ 
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
    var regexS = "[\\?&]"+name+"=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    var results = regex.exec(window.location.href); 
    if(results == null) { 
     return ""; 
    } else { 
     return results[1]; 
    } 
} 

而且使用這樣的:

function getid() { 
    var id = gup('id'); 
    if (id != '') { 
     document.getElementById(id).style.display = 'block'; 
    } 
} 
+0

哇!你的JavaScript代碼簡單的HTML工作就像一個魅力 你救了我達林,非常感謝:) – 2010-08-20 14:19:14