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

发布于 2024-09-15 19:01:55 字数 1648 浏览 6 评论 0原文

我使用以下代码打开一个弹出窗口并传递 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>

场景是,我需要在弹出窗口中显示 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>

现在,弹出窗口给出错误“请求未定义”。

I am using the following code to open a popup window and passing the ID of as query string.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2024-09-22 19:01:55

您将 ASP.NET 服务器端语言与 javascript 混合在一起,这当然是不可能的。尝试这样:

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

如果您没有使用服务器端语言,您可以使用以下函数读取 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];
    }
}

并像这样使用:

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

You are mixing ASP.NET server side language with javascript which of course is not possible. 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):

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';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文