简体   繁体   中英

How WebConfig appsetting value can be called inside a window.open function ASP.NET?

<input id="Button1" type="button" value="button" onclick='window.open("https://google.com")' />

I have to change this using webconfig appsettings.

In webconfig, I have

<add key="Google" value="https://google.com"/>

I have to get the url webconfig using the key.

I have tried

<input id="Button1" type="button" value="button" onclick='window.open("<%= ConfigurationManager.AppSettings["Google"] %>")' />

But it is not working.

Could you find a solution to access the webconfig appsettings values in window.open function?

Try using a variable in the code behind, for example

string openUrl = ConfigurationManager.AppSettings["Google"];

And then in the page

<input id="Button1" type="button" value="button" onclick='window.open("<%= openUrl %>")' />

EDIT - based on the comment of wanting to do it in the aspx page itself (not sure why you would want to do this, but I'm sure you have your reasons).

<% string openUrl = System.Configuration.ConfigurationManager.AppSettings["Google"]; %>
<input id="Button1" type="button" value="button" onclick='window.open("<%= openUrl %>")' />

Use JS method:

function openUrl(url) {
  var newWind = window.open(url, '_blank');
  newWind.focus();
}

and:

<input id="Button1" type="button" value="button" onclick='openUrl("<%= ConfigurationManager.AppSettings["Google"].ToString() %>")' />

Or read key in JS

function openUrl() {
      var url = '<%=ConfigurationManager.AppSettings["Google"].ToString() %>';
      var newWind = window.open(url, '_blank');
      newWind.focus();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM