English 中文(简体)
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.

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

最佳回答

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 ;
    }
}
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签