Access to the query string / parameter from the html page

I have a html base page where the request parameter is passed to the page. I thought I could get the value using Request.QueryString, but the more I read, the better it is only for asp applications. My html looks 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> 

Am I calling a page with displayPdf.htm? path = scan.pdf

I was looking for a way to access request parameters in html without having to write a javascript solution, but had no luck. I am sure that adding the js function will not be too big a deal, I would just think that I would avoid this if there was a single line approach.

Thanks for the help.

+6
source share
1 answer

This cannot be done in pure HTML.

There are two ways to do this 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 to populate the parameter using the 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 any case, the query string is read from the location.search property and parsed using a simple regular expression.

+8
source

Source: https://habr.com/ru/post/912596/


All Articles