3

how to call Request.QueryString inside javascript function and i am using asp.net and C#

var str=<%=Request.quesryString("val")%>

but it is giving me error

शेखर
  • 17,412
  • 13
  • 61
  • 117
navya
  • 144
  • 1
  • 1
  • 10

7 Answers7

8

If this javascript code is inline in your webform the correct way is to use a javascript serializer:

<script type="text/javascript">
    var str = <%= new JavaScriptSerializer().Serialize(Request.QueryString["val"]) %>;
    alert(str);
</script>

Never do the following, it's completely unsafe and your site vulnerable to XSS injection attacks:

<script type="text/javascript">
    var str = '<%= Request.QueryString["val"] %>';
    alert(str);
</script>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

You can do it with JavaScript directly.

With window.location.search you get full queryString like ?item1=val1&item2=val2

You can get querystring using some like

window.location.search.substr(1)

and use .split('&') or substring function to get value of de 'val' key or another

1

You can actually write a function in javascript and pass the query string as parameter from the code behind. For example I have this textbox I want to set the value to the query

function searchTerm(text) {
        document.getElementById('txtSearch').value = text;
}

So in my code I use ScriptManager to call the javascript function and pass the query that way

ScriptManager.RegisterStartupScript(Page, typeof(Page), "", "searchTerm('" + Request.QueryString["id"].ToString() + "')", true);
Zahir
  • 93
  • 3
  • 11
0

Leveraging the theme of security in requests in ASP NET...

You can also use JavaScriptStringEncode() or the AntiXSS library to prevent XSS in your website.

Tiago Crizanto
  • 314
  • 8
  • 22
0

You are doing it wrong use this code to get the QueryString value because it isn't just giving you a Query String in JavaScript it will convert this into an object so use $.each function to retrieve the value of Query String

 let id = $(<%= Request.QueryString["id"] %>);
                    id.each((index, value) => {
                        console.log(value);
                    })

It will return you a value of your Query String Thanks

Mohammad Ahmed
  • 1,544
  • 2
  • 9
  • 12
0

use this:

var urlParams = new URLSearchParams(window.location.search);

console.log(urlParams.has('someValue')); // true

console.log(urlParams.get('someValue')); // "value"

it's key sensitive

Community
  • 1
  • 1
-1

Short answer: You can't. The code in the <%= %> block is evaluated on the server side, the JavaScript code is evaluated on the client side.

Long answer: You CAN use inline C# code to generate JavaScript code, which sometimes does make sense. But this is not the same thing as calling C# code from a JavaScipt function. Of course you can only do this in .aspx / .cshtml etc. files, not in .js files, because those are only treated as content and not parsed by ASP.NET.

[EDIT] In your situation there is no need to bother anyway, because you can get the query string through JS anyway, see How can I get query string values in JavaScript?

[EDIT2]: Of course the other answer about the JavaScriptSerializer is absoultely correct concerning safety. If you use the Razor view engine, things get a lot easier and safer because it escapes strings by default. But like I said, in the concrete case, there's no need to bother either way.

Community
  • 1
  • 1
chris
  • 2,541
  • 1
  • 23
  • 40
  • Whilst you are correct, `QueryString` is perfectly callable on the client side. Regardless of whether it's executed on server or client, the call will produce the same (and desired) result. – Mathew Thompson Mar 28 '13 at 10:51
  • @mattytommo you are misleading in saying that QueryString is perfectly callable on the client side. It's a server function. The results from such a server function call may be passes to the client in the form of a generated piece of javascript, as this answer states. – James Mar 28 '13 at 11:05
  • @ManishMishra: I didn't say it was a good idea in the concrete case. See edit for clarification. – chris Mar 28 '13 at 11:11
  • @James: Correct, but you CAN get the query string with pure JS with no need to inject dynamically generated JS code through ASP.NET. – chris Mar 28 '13 at 11:12
  • @James: No, that's not what he said. He (quite correctly) said that not properly escaping dynamically generated JS code is dangerous. I was talking about not using server-generated JS at all. – chris Mar 28 '13 at 11:20
  • @chris you're right I didn't think it through, as long as eval isn't involved it's safe to use window.location.search et al. – James Mar 28 '13 at 11:23