Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing query string/parameter from html page

I have a basic html page which is having a query parameter being passed to the page. I thought I would be able to get the value with Request.QueryString but the more reading I'm doing seems that this is only for asp applications. My html is as follows below

<body>
    <object type="application/pdf" width="100%" height="100%">
        <!--This didn't work-->
        <param name="src" value="<%=Request.QueryString["path"]%>" />
        <!--Neither did this-->
        <!--<param name="src" value="<%=Request.Params["path"]%>" />-->
        <!--When it is hardcoded then my page is displaying the pdf as expected.-->
        <!--<param name="src" value="scan.pdf" />-->
    </object>
</body>

I am calling the page with displayPdf.htm?path=scan.pdf

I've been searching for a way to access query parameters in html without having to write a javascript solution but haven't had any luck. I'm sure adding a js function wouldn't be too big a deal just thought I would avoid it if there was a single line approach.

Thanks for the help.

like image 263
Jeff Fol Avatar asked Apr 06 '12 12:04

Jeff Fol


People also ask

How do I get query string in HTML?

HTML defines three ways a user agent can generate the query string: an HTML form via the <form.auto.>...</form.auto.> element. a server-side image map via the ismap attribute on the <img> element with an <img ismap> construction. an indexed search via the now deprecated <isindex> element.

How do you access parameters in query string?

Another way to access query string parameters is parsing them using the querystring builtin Node. js module. This method, however, must be passed just a querystring portion of a url. Passing it the whole url, like you did in the url.

How do I get parameters in HTML?

For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.

How do you get query string information from a URL?

The PHP Server object provides access to the query string for a page URL. Add the following code to retrieve it: $query_string = $_SERVER['QUERY_STRING']; This code stores the query string in a variable, having retrieved it from the Server object.


1 Answers

This cannot be done in pure HTML.

This can be done in two ways, using JavaScript:

<body><script>
var param = /[&?]path=([^&]+)/.exec(location.search);
param = param ? param[1].replace(/"/g, '&quot;') : '';
document.write('<object type="application/pdf" width="100%" height="100%">\n' +
    '<param name="src" value="' + param + '" />\n</object>');
</script></body>

An alternative method is populating the parameter using DOM (demo: http://jsfiddle.net/N2GUf/2/):

<body><object type="application/pdf" width="100%" height="100%">
    <param name="src" value="" id="param-path" />
</object>
<script>
var param = /[&?]path=([^&]+)/.exec(location.search);
if (param)
    document.getElementById('param-path').value = param[1];
</script>​</body>

In either case, the query string is read from the location.search property, and parsed using a simple regular expression.

like image 152
Rob W Avatar answered Oct 23 '22 14:10

Rob W