简体   繁体   English

使用Request.QueryString()获取可变值时出现未定义错误

[英]Getting undefined error when using Request.QueryString() to get variuable value

I am using the following code to open a popup window and passing the ID of as query string. 我正在使用以下代码打开一个弹出窗口,并将ID作为查询字符串传递。

<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>

The scenario is, i need to show the DIV in popup window whose id is similar to querystring value. 场景是,我需要在弹出窗口中显示其ID类似于querystring值的DIV。 Popup window code is 弹出窗口代码为

<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>

Now, Popup window is giving error "Request is undefined". 现在,弹出窗口给出错误“请求未定义”。

You are mixing ASP.NET server side language with javascript which of course is not possible. 您正在将ASP.NET服务器端语言与javascript混合使用,这当然是不可能的。 Try like this: 尝试这样:

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

If you are not using a server side language you could use the following function to read query string parameters in javascript ( taken from here ): 如果您未使用服务器端语言,则可以使用以下函数读取javascript中的查询字符串参数( 取自此处 ):

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];
    }
}

And use like this: 像这样使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM